How can I properly save and manipulate data in MATLAB using text files?

  • Context: MATLAB 
  • Thread starter Thread starter Saladsamurai
  • Start date Start date
  • Tags Tags
    Data File Matlab
Click For Summary

Discussion Overview

The discussion revolves around saving and manipulating data in MATLAB using text files. Participants explore methods for loading data from text files, performing operations on the data, and saving the results in a desired format.

Discussion Character

  • Technical explanation, Conceptual clarification, Debate/contested, Homework-related

Main Points Raised

  • Casey describes an attempt to load a matrix from a text file, double it, and save it, but encounters issues with the output file being mostly empty.
  • Casey later finds a solution using the fprintf function to write the modified matrix to a new text file but expresses confusion about the syntax and functionality of fprintf.
  • Another participant explains that fprintf is a standard output function and notes that the default load and save commands in MATLAB deal with binary files, suggesting the use of the -ascii option for saving text files.
  • One participant recommends using dlmread instead of load for handling text files, highlighting its flexibility with delimiters and the ability to skip headers.

Areas of Agreement / Disagreement

There is no consensus on the best method for saving and manipulating data in MATLAB, as participants present different approaches and tools, indicating multiple competing views.

Contextual Notes

Participants mention the limitations of default MATLAB commands regarding file formats and the need for specific syntax when using fprintf. There is also a note on the potential need to handle headers in text files.

Who May Find This Useful

Readers interested in data manipulation in MATLAB, particularly those working with text files and matrix operations.

Saladsamurai
Messages
3,009
Reaction score
7
How do I make this happen?



Code:
E=load('elements.txt');
EE=2*E;
save('poop.txt','EE')

It loads elements.txt
doubles the matrix
stores the results in a new text file

this does not work. It creates a new txt file but there is not much in it besides
the "created on 'this date & time' info"

I am having trouble deciphering which command I should be using
for this.

Any ideas?
Thanks,
Casey
 
Physics news on Phys.org
Okay, I got it using this:
Code:
E=load('elements.txt');
EE=3*E;
fid=fopen('poop.txt','w');
fprintf(fid,'   %8.2f    %8.2f   %8.2f   %8.2f     %8.2f\n ',EE)
fclose(fid);

However, this line is what is getting me. I don't know what it means or understand the syntax:
Code:
fprintf(fid,'   %8.2f    %8.2f   %8.2f   %8.2f     %8.2f\n ',EE)

the imported file is a 20 x 5 matrix

then it is multiplied by a scalar and exported to a new file.

I would like the new file to look like a new 20 x 5 matrix.

How do I do that?

Thanks,
Casey
 
fprintf is just the standard C style output function (in this case, it's outputting to the file specified by fid instead of to the screen):
http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fprintf.html

The problem with your approach is that, by default, the 'load' and 'save' commands load and save binary files that encode some matrix. If you use the -ascii option, it'd save more along the lines of what you're trying to do (though you can always use the C-style fprintf and fscanf):
http://www.mathworks.com/access/helpdesk/help/techdoc/ref/load.html
http://www.mathworks.com/access/helpdesk/help/techdoc/ref/save.html
 
It is usually better to use "dlmread" instead of "load" when handling text files, the former let's you define which delimiter is used (e.g. space or tab) and you can also skip the the first few columns or lines. This is very useful if the file has a header (e.g. the names of the columns).
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
7K
  • · Replies 2 ·
Replies
2
Views
5K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
Replies
1
Views
2K
  • · Replies 7 ·
Replies
7
Views
6K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
1
Views
3K
Replies
1
Views
6K