Way to output a matrix or dat file

6 views (last 30 days)
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
Walter Roberson
Walter Roberson on 16 Aug 2011
{'XYZ' 'ABC' 5; 'ABC' 'EFG' 3}
Jan
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.

Sign in to comment.

Accepted Answer

Rick Rosson
Rick Rosson on 17 Aug 2011
I am not sure if csvwrite will work with strings. You may need to use xlswrite instead, or you could try to create your own customized function using fprintf. For example:
function myFileExport(filename,A)
csv = fopen(filename,'w');
fprintf(csv, ... );
fprintf(csv, ... );
...
fclose(csv);
end
HTH.
Rick

More Answers (4)

Rick Rosson
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
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
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
Chris on 16 Aug 2011
Thanks everyone for your help. I noticed that if I removed the parfor command and made it a for loop instead I was able to use the cell matrix that Jan recommended.
I like the idea of the cvswrite. I tried this but for some reason when I write to the file it takes a text like ABC and write is like A,B,C. This is a newb question but how do I keep the ABC together and have it put the comma after the text

Categories

Find more on MATLAB in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!