MATLAB how to automatically read a number of files

Click For Summary
SUMMARY

The discussion focuses on automating the reading of multiple data files in MATLAB for plotting 3D graphs. The user seeks a solution to dynamically generate file names, specifically addressing the formatting issue with varying spaces in file names. The recommended approach involves using the sprintf function within a loop to construct the file names correctly. The provided code snippet demonstrates how to iterate through file names from 'SS 1.dat' to 'SS 10.dat' while ensuring proper formatting.

PREREQUISITES
  • Familiarity with MATLAB programming
  • Understanding of file I/O operations in MATLAB
  • Basic knowledge of 3D plotting in MATLAB
  • Experience with loops and string manipulation in MATLAB
NEXT STEPS
  • Explore MATLAB's fopen function for advanced file handling
  • Learn about MATLAB's 3D plotting functions such as plot3 and surf
  • Investigate string formatting techniques in MATLAB using sprint and strcat
  • Study error handling in MATLAB file operations to manage missing files
USEFUL FOR

This discussion is beneficial for MATLAB users, data analysts, and researchers who need to automate the processing of multiple data files for visualization purposes.

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

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
  • · 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