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

  • Thread starter peterjaybee
  • Start date
  • Tags
    Matrix
In summary, the individual is trying to automate the process of saving multiple experiments in MATLAB using a variable for the file name. They are wondering if there is a similar method to do so as in Mathematica, or if they have to change the file name manually each time. Suggestions for using int2str or num2str to concatenate the file name with the experiment number are provided as well as the use of the "eval" command. The individual is also advised to consider using fprintf or num2str for leading zeros in the file name.
  • #1
peterjaybee
62
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
  • #2


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:

1. What is a matrix?

A matrix is a collection of numbers arranged in rows and columns. It is often used in mathematics and science to represent data and perform calculations.

2. How do I save a matrix in my script?

You can save a matrix in your script by assigning it to a variable using the equal sign (=) followed by the values of the matrix enclosed in square brackets ([]). For example: matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];

3. Can I perform calculations with a matrix in my script?

Yes, you can perform calculations with a matrix in your script using mathematical operators like addition (+), subtraction (-), multiplication (*), and division (/). You can also use built-in functions or libraries to perform more complex operations.

4. How do I access specific elements in a matrix?

To access specific elements in a matrix, you can use the index notation [row][column]. This will return the value at the specified row and column. Remember that indexing starts at 0, so the first row or column is represented by 0, the second by 1, and so on.

5. Can I store different types of data in a matrix?

Yes, you can store different types of data in a matrix, such as numbers, strings, or even other matrices. However, it is important to ensure that your matrix is structured and all elements are of the same type to avoid errors when performing calculations or operations.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • Programming and Computer Science
Replies
9
Views
850
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
13K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
11K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
Back
Top