Atlab I am trying to write a script and currently I save a matrix

  • Thread starter Thread starter peterjaybee
  • Start date Start date
  • Tags Tags
    Matrix
Click For Summary
SUMMARY

This discussion focuses on saving matrices to text files in MATLAB, specifically how to dynamically name files based on a variable. The user successfully saves a matrix using the command save('expt1.txt', 'table', '-ascii') but seeks to incorporate a variable expt_no into the filename. The solution provided involves using save(['expt', int2str(exptnum), '.txt'], 'table', '-ascii') to concatenate the filename with the experiment number. For formatting filenames with leading zeros, fprintf or num2str is recommended.

PREREQUISITES
  • Familiarity with MATLAB scripting
  • Understanding of matrix data structures in MATLAB
  • Knowledge of file I/O operations in MATLAB
  • Basic string manipulation techniques in programming
NEXT STEPS
  • Learn how to use fprintf for formatted output in MATLAB
  • Explore the num2str function for converting numbers to strings
  • Investigate the eval function and its applications in MATLAB
  • Review MATLAB documentation on file handling and naming conventions
USEFUL FOR

MATLAB users, data analysts, and researchers who need to automate the saving of multiple experimental results with dynamic filenames.

peterjaybee
Messages
62
Reaction score
0
Hi, in MATLAB I am trying to write a script and currently I save a matrix to a .txt file using save('expt1.txt', 'table', '-ascii'). 'table' is the matrix I want to write to file.

This works fine, but I would like to make the file name to be 'exptn.txt', where n is a variable specified earlier in the script expt_no = n

In mathematica i could imput code in the following way.

"expt"<>expt_no<>".txt"

Can something similar be done in MATLAB or do I just have to change it manually each time?
 
Physics news on Phys.org


If you've got a script that runs through multiple experiments that you'd like saved, you could concatenate your textfile name along with the experiment numbering using int2str or num2str, for instance:

>>save(['expt', int2str(exptnum),'.txt'], 'table', '-ascii')

http://www.mathworks.com/help/techdoc/ref/int2str.html

There's also a nifty command (eval) that allows you to execute a string, but that's a rather inelegant way of doing what you seek to do:
http://www.mathworks.com/help/techdoc/ref/eval.html

EDIT: I've also asked a mod if they'd move this to the Math and Science Software sub-forum, where you might be able to find similar-type questions (for future reference)

Additionally, if you need leading zeros (e.g. expt001, expt010, expt100) you might be better off using fprintf or num2str, as mentioned in the documentation page linked above)
 
Last edited:

Similar threads

  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
3
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
7K
  • · Replies 4 ·
Replies
4
Views
14K
Replies
3
Views
3K
Replies
2
Views
2K
  • · Replies 52 ·
2
Replies
52
Views
13K