MATLAB how to automatically read a number of files

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.
 
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

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 12 ·
Replies
12
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
5K
Replies
2
Views
3K
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
6K