MATLAB how to automatically read a number of files

5 views (last 30 days)
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.

Answers (1)

Stephen23
Stephen23 on 29 Oct 2015
Edited: Stephen23 on 30 Oct 2015
This is a very common question, so we covered it very thoroughly in our wiki:
Because the files already exist I would recommend that you use the dir method, rather than trying to recreate the filenames using sprintf. You can use my FEX submission to sort the filenames correctly:
Or alternatively using sprint you can adapt this for your code:
>> fmt = 'SS%4d.dat';
>> sprintf(fmt,9)
ans = SS 9.dat
>> sprintf(fmt,10)
ans = SS 10.dat

Categories

Find more on Printing and Saving in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!