How can save to a matrix to txt file
1 view (last 30 days)
Show older comments
Hi ,
I have a matrix (1081x72) in txt file. And I opened it via Microsoft Excel and save it xls format. And, Read this matrix from xls.Then transposed it. I want to save this transposed matrix to txt file. But when I try to save this matrix , it isn't showed properly. These are my input and outfile and codes.
A = xlsread('armstrong-2002-v1_database.xls')
B = transpose(A);
dlmwrite('myFile.txt',B,'delimiter',' ');
0 Comments
Answers (3)
Walter Roberson
on 9 Oct 2015
I checked the .xls file and I checked the .txt file, and I do not see anything obvious wrong. All of the numeric values are there.
None of the header information is written out, but when you use xlsread() the first output only reflects the numeric content so it would not be expected that the headers would be written out. If you want to write out the headers as well, you should be using the three-output version of xlsread() and writing out the third of them:
[num, txt, raw] = xlsread('armstrong-2002-v1_database.xls');
And then you have to write out raw
A difficulty with writing out raw is that dlmwrite() only handles pure numeric values. You need to do some transformations on it and write it out as text:
B = raw;
B(cellfun(@isnan,B)) = {'-'}; %do not make it blank or empty or you will have trouble reading the file back in
idx = cellfun(@(C) ~isempty(C) && isnumeric(C(1)), B);
B(idx) = cellfun(@(V) sprintf('%g',V), B(idx), 'Uniform', 0);
%notice we have not transposed yet
fid = fopen('myFile.txt', 'wt');
%here's a trick: when you fprintf, it takes values down columns instead of across
%across rows. Normally you deal with that by transposing your data before sending it
%to fprintf. But we already have it in transposed form, so we can send it directly.
fmt = [repmat('%s ', 1, size(B,1)-1), '%s\n'];
fprintf(fid, B{:});
fclose(fid);
4 Comments
Walter Roberson
on 9 Oct 2015
A = xlsread('armstrong-2002-v1_database.xls')
B = transpose(A);
dlmwrite('myFile.txt', B, 'delimiter',' ', 'newline', 'pc');
Stephen23
on 9 Oct 2015
Edited: Stephen23
on 9 Oct 2015
The problem is not the file or the code, the problem is the text editor that you are using. Microsoft Notepad is very poor at handling large text files and particularly with handling long text rows. It is also notorious for mismanaging newline characters.
It seems that Notepad cannot display your file properly.
When I open your file in Notepad++ (a much better text editor) it looks fine. and matches exactly the transposed .xls data. You should upgrade your text editor to something more robust than Microsoft's Notepad, such as Notepad++.
Here is your text file shown in Notepad++ without word wrapping:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/178652/image.png)
and shown in Notepad++ with word wrapping:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/178653/image.png)
See Also
Categories
Find more on Text Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!