How to read a data from a text file. I want to read X and Y values from this text file and save them?

9 views (last 30 days)

Accepted Answer

Ameer Hamza
Ameer Hamza on 14 May 2020
Run these lines
f = fopen('test.txt');
data = textscan(f,'X="%f" Y="%f" ZOOM="%f"','HeaderLines', 1);
fclose(f);
data = [data{:}];
2 Comments
Ameer Hamza
Ameer Hamza on 14 May 2020
Instead of saving in separate variables, use cell array. Read here why: //www.tatmou.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval . Try this code.
f = fopen('test.txt');
data = {};
whiletrue
d = textscan(f,'X="%f" Y="%f" ZOOM="%f"','HeaderLines', 1);
d = [d{:}];
ifisempty(d)
break;
end
data{end+1} = d;
end
fclose(f);

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!