MATLAB how to automatically read a number of files

In summary, To plot multiple 3D graphs from different data files, you can use the fopen function to read each file and then plot a graph. To automatically change the file name for each iteration, you can use sprintf to construct the file name string with a loop variable. Additionally, for file names with only two spaces between SS and the number, you can use sprintf to adjust for this difference.
  • #1
kelvin490
Gold Member
228
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
  • #2
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 kelvin490

1. How can I use MATLAB to read multiple files automatically?

To automatically read a number of files in MATLAB, you can use the "dir" function to get a list of all the files in a directory, and then use a for loop to iterate through the list and read each file using the "load" function. You can also use the "importdata" function to read multiple files at once.

2. Can I specify certain file types to be read automatically in MATLAB?

Yes, you can use the "dir" function with a wildcard character (*) to specify certain file types to be read automatically. For example, if you only want to read files with a .txt extension, you can use "dir('*.txt')" to get a list of those files.

3. How can I store the data from the files I read automatically in a variable?

You can use the "load" function to read the data from each file and store it in a variable. If you are using the "importdata" function, the data will already be stored in a variable called "data". You can also use the "cat" function to concatenate the data from multiple files into one variable.

4. Is there a way to automatically read files from multiple directories in MATLAB?

Yes, you can use the "dir" function with a file path to specify a specific directory to read files from. You can also use a for loop to iterate through multiple directories and read files from each one.

5. Can I set conditions for which files are automatically read in MATLAB?

Yes, you can use logical operators and conditional statements within your for loop to specify conditions for which files to read. For example, you can use an if statement to only read files that meet a certain criteria, such as a specific file name or file size.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
6K
  • Programming and Computer Science
Replies
4
Views
747
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
Back
Top