Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

MATLAB how to automatically read a number of files

  1. Oct 29, 2015 #1


    User Avatar
    Gold Member

    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.
  2. jcsd
  3. Oct 29, 2015 #2


    User Avatar
    Gold Member

    Use sprintf to construct the file name string using text + a loop variable:

    Code (Text):

    for n=1:10
        file = sprintf('SS %d.dat',n)
        fid = fopen(file,'r');
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Similar Threads - MATLAB automatically read Date
How to program this in Matlab Feb 28, 2018
Matlab Making a short test for self-adjointness Feb 23, 2018
Matlab Simulink: designing a 2nd order sliding controller Jul 5, 2017
Select variable automatically in matlab May 20, 2012