How can save to a matrix to txt file

조회 수: 2 (최근 30일)
as nem
as nem 2015년 10월 9일
댓글: Walter Roberson 2015년 10월 9일
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',' ');

답변 (3개)

Walter Roberson
Walter Roberson 2015년 10월 9일
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
as nem
as nem 2015년 10월 9일
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 2015년 10월 9일
A = xlsread('armstrong-2002-v1_database.xls')
B = transpose(A);
dlmwrite('myFile.txt', B, 'delimiter',' ', 'newline', 'pc');

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


Stalin Samuel
Stalin Samuel 2015년 10월 9일
dlmwrite('yourfile.txt',B,'-append','roffset',1,'delimiter',' ')
  댓글 수: 1
as nem
as nem 2015년 10월 9일
편집: as nem 2015년 10월 9일
It doesn't work. I try it but Myfile.txt looks like this

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


Stephen23
Stephen23 2015년 10월 9일
편집: Stephen23 2015년 10월 9일
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
as nem
as nem 2015년 10월 9일
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.

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

카테고리

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