format data into string and write a txt file

조회 수: 13 (최근 30일)
FM
FM 2018년 10월 24일
편집: Stephen23 2018년 10월 25일
Hi,
I have the following cell,
a={[1E5 0 0 7.82E12 0]
[-1 0 0 -1 0]};
and I want to write a text file with the following format
1.0000000000000000E+05 0.0000000000000000E+00 0.0000000000000000E+00 7.8200000000000000E+12 0.0000000000000000E+00
-1.0000000000000000E+00 0.0000000000000000E+00 0.0000000000000000E+00 -1.0000000000000000E+00 0.0000000000000000E+00
As in two blank spaces at the beginning of the line if the first value is positive (line 1) and one blank space if it is negative (line 2), the "-" uses one space. There are also two blank spaces in between the values (if the value is negative the "-" uses one of those blank spaces).
First I tried converting a to a string cell:
for i=1:2
a{i}=num2str(a{i},'%.16e');
end
And I get this:
a =
2×1 cell array
{'1.0000000000000000e+050.0000000000000000e+000.0000000000000000e+007.8200000000000000e+120.0000000000000000e+00' }
{'-1.0000000000000000e+00 0.0000000000000000e+00 0.0000000000000000e+00-1.0000000000000000e+00 0.0000000000000000e+00'}
There are no blank spaces at all. Is it possible to specify the number of blank spaces in between the values?
Thank you,

채택된 답변

Stephen23
Stephen23 2018년 10월 25일
편집: Stephen23 2018년 10월 25일
A cell array of numeric vectors is quite inconvenient to work with, so the first thing to do is convert it to a much easier to use numeric matrix:
a = {[1E5,0,0,7.82E12,0],[-1,0,0,-1,0]};
m = vertcat(a{:});
Method one: dlmwrite:
dlmwrite('sample.txt', m, 'delimiter',' ', 'precision','% .16E')
Method two: fprintf:
fmt = repmat(' % .16E',1,size(m,2));
fmt = [fmt(2:end),'\n'];
fprintf(fmt,m.')
prints this:
1.0000000000000000E+005 0.0000000000000000E+000 0.0000000000000000E+000 7.8200000000000000E+012 0.0000000000000000E+000
-1.0000000000000000E+000 0.0000000000000000E+000 0.0000000000000000E+000 -1.0000000000000000E+000 0.0000000000000000E+000
To write to a text file, add fopen and fclose:
[fid,msg] = fopen('sample.txt','wt');
assert(fid>=3,msg)
fprintf(fid,fmt,m.')
fclose(fid)
  댓글 수: 3
FM
FM 2018년 10월 25일
Oh I just modified fmt like this and it works!
fmt = [' ',fmt(2:end),'\n'];
Stephen23
Stephen23 2018년 10월 25일
편집: Stephen23 2018년 10월 25일
"Is there a way I can add one blank space at the beginning of each line in the text file? That is the only missing"
Of course, that is easy (I removed it, because it is missing from your example data shown on this page. This is why it is always better to upload a sample file, rather than relying on the markup of this page, which can change displayed characters). Use these:
Method one:
'precision',' % .16E'
Method two:
fmt = [repmat(' % .16E',1,size(m,2)),'\n'];
Take another look at your proposed solution:
fmt = [' ',fmt(2:end),'\n'];
The fmt char vector's first character was a space character (as defined inside the repmat), so why remove that space character, just to add another space character?

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

추가 답변 (1개)

madhan ravi
madhan ravi 2018년 10월 24일
편집: madhan ravi 2018년 10월 25일
a={[1E5 0 0 7.82E12 0]
[-1 0 0 -1 0]};
dlmwrite('sample.txt',a,'delimiter','\t','precision','%0.16e')
  댓글 수: 8
madhan ravi
madhan ravi 2018년 10월 25일
see attached file
FM
FM 2018년 10월 25일
I copied pasted but you wrote and it gives me a text file with only one line of text, I changed the file extension .txt to .asc (which is ultimately the file extension I need) and now it works, I have two lines of text, thanks!
_thats how you can distinguish the values right??_
yes but the file I am writing is to be read by another software and unless I respect a specific format the other software is unable to read the file. That's why I need two blank spaces in between the values

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

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

제품


릴리스

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by