Way to output a matrix or dat file
Show older comments
I am new to matlab, and trying to figure out a way to ouput. I would like to output into a data file or matrix variable. I have it calculating in a for loop because it goes through a matrix of symbols I have.
symbol1 symbol2 egcitestpvalue
It doesn't appear to like when I try to mix and match numerical data with text. What is the best way to have it output versus having it go to the command window. I would like some sort of grid I can export, sort in excel, etc.
4 Comments
Jan
on 16 Aug 2011
Please explain the number, type and dimensions of the variable you want to write to the file. Then specify how you want to read the file later, from MATLAB, Excel or a text editor?
Chris
on 16 Aug 2011
Walter Roberson
on 16 Aug 2011
{'XYZ' 'ABC' 5; 'ABC' 'EFG' 3}
Jan
on 16 Aug 2011
@Chris: Look for "cell" in the documentation for an array type, which can store objects of different size and type.
Please post an code example for your question about the dynamically sized variable.
Accepted Answer
More Answers (4)
Rick Rosson
on 16 Aug 2011
Hi Chris,
There are several different ways to accomplish what you are asking.
One option is to export your data to MS Excel using either csvwrite or xlswrite. For more information about these two functions:
>> doc csvwrite
OR
>> doc xlswrite
HTH.
Rick
Rick Rosson
on 16 Aug 2011
Another option is to use fprintf to print formatted output to a text file rather than to the Command Window. So, for example:
x = pi;
filename = 'myfile.txt';
txt = fopen(filename,'w');
fprintf(txt,'The value of variable x is: %15.9f \n',x);
...
...
fclose(txt);
For more information:
>> doc fprintf
HTH.
Rick
Rick Rosson
on 16 Aug 2011
A third option, if you want very nicely formatted output in the form of an HTML or PDF document, would be to use the publish command. This approach would require you to create a simple MATLAB script using "cell mode", comments, and commands that display your data to the Command Window. You can then call the publish command to create a formatted document in a variety of standard formats, including HTML and PDF.
For more information:
>> doc publish
HTH.
Rick
Chris
on 16 Aug 2011
0 votes
Categories
Find more on Data Import from MATLAB in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!