dlmwrite help
조회 수: 1 (최근 30일)
이전 댓글 표시
I can't use xlswrite because i have a mac so can anyone give a example of how to use dlmwrite if i have a column of text?
댓글 수: 0
채택된 답변
Walter Roberson
2011년 11월 1일
On systems that are not running MS Windows, and on MS Windows systems that do not have Excel installed, xlswrite() is restricted to "basic" mode, which can only handle numeric arrays.
by submitting an array of pure characters and using a character format in the Precision parameter and setting the delimiter to be empty.
That would look like
dlmwrite(YourFilename, YourCharacterArray, 'delimiter', '', 'precision', '%c')
where YourCharacterArray is already fully converted to characters.
Referring to your earlier posting in which you had F, a column of words, and G, a column of numbers, that would be like
nrows = size(G,1);
YourCharacterArray = [repmat('"', nrows, 1), F, repmat('",',nrows,1), num2str(G)];
The above includes quoting the words and putting in the comma delimiter between the words and the numbers.
But if you are going to go through all this trouble to prepare everything as character ahead of time, all you have gained is incomprehensibility and slowness of your code, and all you have removed is one explicit file open and close. Better, likely, to use the low-level I/O operations recommended in the user guide
fid = fopen(YourFileName, 'wt');
for K = 1 : size(G,1)
fprintf(fid, '"%s",%f\n', F(K,:), G(K));
end
fclose(fid)
That is 5 easy lines that nearly any programmer will understand, compared to 3 lines for the dlmwrite() that relatively few people will understand (and those who do understand them will wonder why you didn't just use fprintf() !)
Going the dlmwrite() route is possible, but will cause more confusion than it solves problems. The only reason I had for inventing the technique a few years back was that other people said that it couldn't be done.
댓글 수: 2
Walter Roberson
2011년 11월 2일
csvwrite() writes text files by calling dlmwrite(), which calls fopen() and fprintf() and fclose(). csv (command separated value) text files can be read by any version of Excel I have ever used (which is basically every version of Excel that has ever been published... except maybe I missed the very first version.)
You just go in to Excel, and you File->Open the .csv text file, and Excel will read it in. .csv files are plain text, that are exactly like they look in an editor.
추가 답변 (1개)
bym
2011년 11월 1일
I don't think you can, try
fprintf()
댓글 수: 2
Walter Roberson
2011년 11월 1일
That is not correct, Abra. Excel can read csv (text) files, and fprintf() is an important technique for creating text files.
If you are imagining that you can get MATLAB to start up Excel on OS-X and trigger Excel to load the file, and do something with it, then you would have to get in to AppleScript, which you really would rather not do when you can simply fprintf() your file.
dlmwrite() uses fprintf() internally. You can look at the dlmwrite() source code:
type dlmwrite
참고 항목
카테고리
Help Center 및 File Exchange에서 Spreadsheets에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!