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
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
1 reply · 2K views
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: