Way to output a matrix or dat file

조회 수: 4 (최근 30일)
Chris
Chris 2011년 8월 16일
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
Walter Roberson
Walter Roberson 2011년 8월 16일
{'XYZ' 'ABC' 5; 'ABC' 'EFG' 3}
Jan
Jan 2011년 8월 16일
@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.

댓글을 달려면 로그인하십시오.

채택된 답변

Rick Rosson
Rick Rosson 2011년 8월 17일
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

추가 답변 (4개)

Rick Rosson
Rick Rosson 2011년 8월 16일
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 2011년 8월 16일
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 2011년 8월 16일
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 2011년 8월 16일
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

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by