MATLAB save command and transpose command

In summary: Save(r1)In summary, the conversation discussed the issue of saving row vectors as columns in a text file using MATLAB. The problem arose when trying to use the save function with the transpose operator, resulting in an unbalanced string error. The solution proposed was to create new objects with the transposed data, but this was deemed inefficient. A possible workaround was suggested in the form of a custom function that performs the transpose and save operations in one step.
  • #1
Saladsamurai
3,020
7
I have a bunch of row vectors saved as objects r1 r2 r3. I would like to send each set of data (row) to a text file, but I want it to save as a column. This means that I want to save the transpose of the data, i.e., r1' r2'...

Unfortunately, when I try to use
Code:
save r1.txt r1' -ascii

MATLAB thinks that the quote ' is an unbalanced string.

I know that an "easy fix" would be to create new objects like R1=r1';

but I don't really think that is efficient. How can I get around this?

thanks
Casey
 
Physics news on Phys.org
  • #2
any takers on this one?
 
  • #3
Saladsamurai said:
any takers on this one?

As far as I know, the MATLAB function SAVE expects the following arguments:

save(<string (filename)>, <string (variable name 1)>, ... <string (variable name N)>, <string (options)>)

You are not passing the function actual variables, just the names. That's why you can't pass r1', because that is not a string.


Why is,

r1 = r1';
save('rrrrrs.txt', 'r1', '-ascii');

not efficient?

I mean, the transpose has to be done somewhere in memory.
 
  • #4
FrogPad said:
Why is,

r1 = r1';
save('rrrrrs.txt', 'r1', '-ascii');

not efficient?

I mean, the transpose has to be done somewhere in memory.

Because if I have ten r values, I have to create 10 new R=r' values.

I suppose I could write a for loop that does it for me, but for only 10 entries, that seemed a little tedious (possibly just laziness :smile:).
 
  • #5
lol, yeah.

You could probably write a wrapper around the save function, to do what you want.


function tSave(V)

vt = V';

save([num2str(V) '.txt'], 'vt', '-ascii');

end

then just call this little function instead
 

What is the purpose of the MATLAB save command?

The save command in MATLAB is used to save workspace variables to a file for future use. It allows users to save their work and data in a specific format that can be easily loaded and used in future sessions.

How do I use the MATLAB save command?

To use the save command, first make sure you have the variables you want to save in your workspace. Then, use the syntax "save filename var1 var2 var3..." to specify which variables you want to save. You can also use wildcards to save all variables in your workspace. Finally, specify the file format you want to save in, such as .mat or .csv.

Can I save multiple variables in one file using the MATLAB save command?

Yes, you can save multiple variables in one file using the save command. Simply list all the variables you want to save after the filename, separated by spaces.

What is the purpose of the transpose command in MATLAB?

The transpose command in MATLAB is used to flip the rows and columns of a matrix. It is denoted by an apostrophe (') and can be used to convert a row vector into a column vector and vice versa.

How do I use the transpose command in MATLAB?

To use the transpose command, simply add an apostrophe after the matrix or variable you want to transpose. For example, if A is a matrix, A' will transpose it. You can also use the transpose function, which is equivalent to the apostrophe operator.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
6K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
9K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
Back
Top