MATLAB save command and transpose command

  • Context: MATLAB 
  • Thread starter Thread starter Saladsamurai
  • Start date Start date
  • Tags Tags
    Matlab Transpose
Click For Summary
SUMMARY

The discussion centers on using the MATLAB save command to export row vectors as transposed column vectors in text files. The user, Casey, encounters an issue when attempting to save the transpose of a vector directly using save r1.txt r1' -ascii, which results in an error due to MATLAB interpreting the transpose operator as an unbalanced string. A proposed solution involves creating a wrapper function, tSave(V), that transposes the vector and saves it efficiently without the need to create multiple new variables.

PREREQUISITES
  • Familiarity with MATLAB syntax and commands
  • Understanding of matrix transposition in MATLAB
  • Basic knowledge of function creation in MATLAB
  • Experience with file I/O operations in MATLAB
NEXT STEPS
  • Explore MATLAB's save function documentation for advanced usage
  • Learn about creating and using custom functions in MATLAB
  • Investigate MATLAB's matrix manipulation capabilities, specifically transposition
  • Research efficient data handling techniques in MATLAB for large datasets
USEFUL FOR

This discussion is beneficial for MATLAB users, data analysts, and researchers who need to export data efficiently while maintaining the desired format, particularly those working with matrix operations and file management in MATLAB.

Saladsamurai
Messages
3,009
Reaction score
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
any takers on this one?
 
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.
 
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:).
 
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
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
7K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 3 ·
Replies
3
Views
11K
Replies
1
Views
2K
  • · Replies 7 ·
Replies
7
Views
15K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 1 ·
Replies
1
Views
3K