How can save to a matrix to txt file

6 views (last 30 days)
as nem
as nem on 9 Oct 2015
Commented: Walter Roberson on 9 Oct 2015
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',' ');

Answers (3)

Walter Roberson
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
as nem
as nem on 9 Oct 2015
okey you are right. I have 1081 entries per line and but when I write to matrix to file , successive lines are connected to each other. And I don't use this file. And, How can I set a fixed width in text file ?
Walter Roberson
Walter Roberson on 9 Oct 2015
A = xlsread('armstrong-2002-v1_database.xls')
B = transpose(A);
dlmwrite('myFile.txt', B, 'delimiter',' ', 'newline', 'pc');

Sign in to comment.


Stalin Samuel
Stalin Samuel on 9 Oct 2015
dlmwrite('yourfile.txt',B,'-append','roffset',1,'delimiter',' ')
  1 Comment
as nem
as nem on 9 Oct 2015
Edited: as nem on 9 Oct 2015
It doesn't work. I try it but Myfile.txt looks like this

Sign in to comment.


Stephen23
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:
and shown in Notepad++ with word wrapping:
  1 Comment
as nem
as nem on 9 Oct 2015
okey I installed Notepad++ and it works good. But I want to give this text file to another program which is implemented on java. So, The program can not use this text file. The program can not see this text file like a matrix. I want to solve this problem.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!