MATLAB how to automatically read a number of files

Join the discussion
Registration is free. Start your own thread to ask a follow-up.
1 reply · 2K views
kelvin490
Gold Member
Messages
227
Reaction score
3
I would like to plot a number of 3D graphs from different data files. For example I am using

fid = fopen('SS 1.dat','r');

to read the first file and then plot a graph. How to set the program to change the name to 'SS 2.dat' automatically? Also for the tenth file the name becomes 'SS 10.dat' which has one space less (i.e.only two space between SS and 10) then the first to ninth files. How to set the program to adjust for that? Thank you.
 
Physics news on Phys.org
Use sprintf to construct the file name string using text + a loop variable:

Code:
for n=1:10
    file = sprintf('SS %d.dat',n)
    fid = fopen(file,'r');
end
 
  • Like
Likes   Reactions: kelvin490