Filter löschen
Filter löschen

dlmwrite help

1 Ansicht (letzte 30 Tage)
Abra dog
Abra dog am 1 Nov. 2011
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?

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 1 Nov. 2011
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.
To use dlmwrite() to write text, you have to do as I wrote before:
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 Kommentare
Abra dog
Abra dog am 1 Nov. 2011
Ok i understand what your telling me but i still don't understand how to turn that into a excel compatible file
Walter Roberson
Walter Roberson am 2 Nov. 2011
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

bym
bym am 1 Nov. 2011
I don't think you can, try
fprintf()
  2 Kommentare
Abra dog
Abra dog am 1 Nov. 2011
i'm trying to put it into excel so I can't do that
Walter Roberson
Walter Roberson am 1 Nov. 2011
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

Melden Sie sich an, um zu kommentieren.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by