how do I print a txt file of integers and floats?
이전 댓글 표시
I have tried in vain to write a text file of a mix of ints and floats. Suppose I have A = nx1 ints, and B = nx1 floats I want the file to end up like this: [A B], i.e. just two columns, first column the ints, second column the floats
1 3.232
2 4.333
...
100 5.444
I have tried
dlmwrite(filename,[A B]) and the result is
[A int(B)] === Not what I want
id =fopen(filename)
fprintf(id,'%d %f \n',A,B)
fclose(id);
Result is
A
B
also not what I want How do I get the right format?
채택된 답변
추가 답변 (4개)
Joe Yeh
2016년 10월 4일
Well, you'll have to print it line by line:
for ii = 1:n
fprintf(fid, '%d %f \n', A(ii), B(ii));
end
Guillaume
2016년 10월 4일
I suspect that your dlmwrite call didn't work as expected because A is some integer class, so B gets converted to integer when you concatenate it with A.
Try:
dlmwrite(filename, [double(A) B])
댓글 수: 2
John Petersen
2016년 10월 5일
Walter Roberson
2016년 10월 5일
dlmwrite() allows you to change the output format using the 'Precision' option, but only one format can be specified; you cannot specify different formats for different columns.
John Petersen
2016년 10월 4일
Dheeraj Maurya
2022년 5월 23일
0 개 추천
what is the type =is integer(un)
댓글 수: 1
Walter Roberson
2022년 5월 24일
I am not clear on what you are asking? If you are asking about the return type from the isinteger() call, then the answer would be "logical"
카테고리
도움말 센터 및 File Exchange에서 Structured Data and XML Documents에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!