PDA

View Full Version : Matlab


peterjaybee
Nov18-10, 03:05 PM
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?

MATLABdude
Nov20-10, 04:03 AM
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)