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

In summary: So for this conversation, in summary, the user was trying to load a text file, double the matrix, and save the results in a new text file. They were having trouble understanding the syntax of the "fprintf" command and wanted to know how to export the new matrix in the same format as the original. Suggestions were made to use the "dlmread" function instead of "load" and to use the "-ascii" option with "save".
  • #1
Saladsamurai
3,020
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
  • #2
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
 
  • #3
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
 
  • #4
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).
 

1. What is a MATLAB data file?

A MATLAB data file is a file format used to store data and variables in the MATLAB software. It is commonly used for saving and loading data for analysis and manipulation.

2. How do I create a MATLAB data file?

To create a MATLAB data file, you can use the save function in the MATLAB software. This function allows you to specify the name and location of the file and the variables you want to save. You can also use the saveas function to save the data in a specific file format.

3. What is the difference between a .mat file and a .m file?

A .mat file is a MATLAB data file that stores variables and data, while a .m file is a MATLAB script or function file that contains code. The .mat file can be used to save and load data, while the .m file is used to write and execute code in MATLAB.

4. Can I open a MATLAB data file in other software?

No, MATLAB data files can only be opened and used in the MATLAB software. However, you can export the data from the .mat file to a different file format that can be opened in other software.

5. How do I access data stored in a MATLAB data file?

To access data stored in a MATLAB data file, you can use the load function in the MATLAB software. This function allows you to load the saved variables and data from the file into your MATLAB workspace for further analysis and manipulation.

Similar threads

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