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?

 채택된 답변

Walter Roberson
Walter Roberson 2016년 10월 4일

0 개 추천

This cannot be done with dlmwrite()
If your integer values are small enough to fit within double then you can use
data = [A(:), B(:)] .'; %notice the transpose
fprintf(id, '%d %f\n', data);
If your integer values are small enough to fit within int64 (signed) then you can use
t1 = num2cell(A(:));
t2 = num2cell(B(:));
data = [t1, t2] .'; %notice the transpose
fprintf(id, '%d %f\n', data{:}); %expand the cell. Each argument will have its own data type so the int64 will not get truncated to double
If your integer values are uint64 (unsigned) and might not fit in int64 (signed) then proceed like above with the cells, but use %u instead of %d

댓글 수: 4

once you put mixed data types in a matrix, they change to all the same data type, so the first suggestion doesnt' work. I think the second suggestion is very much the same.
It does not matter for the purpose of %d whether the data is integer data type (such as int16) or double: it will present decimal numbers up to 2^53 . Beyond 2^53 then you do need to worry about whether the integer can be represented exactly as a double.
My second suggestion, using cells, never uses mixed data types. The num2cell() creates individual cell entries for the data, each with appropriate data type. The [t1, t2] creates an N x 2 cell array, each cell having its own data type. The transpose makes it a 2 x N cell array, each cell having its own data type. The {} does comma separated list expansion, reading down the columns first. The result this becomes exactly the same as if you had coded
fprintf(id, '%d %f\n', A(1), B(1), A(2), B(2), A(3), B(3), ...)
and each of those A and B holds its own data type.
Ahhhh, I see now. Thanks Walter, that is a great solution.
This approach works even when some of the variables to be output are cell array of character (what used to be known as strings.)

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

추가 답변 (4개)

Joe Yeh
Joe Yeh 2016년 10월 4일

0 개 추천

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
Guillaume 2016년 10월 4일

0 개 추천

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

yes, that's exactly what happens
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
John Petersen 2016년 10월 4일

0 개 추천

Thank you for your input and suggestions! I have finally come up with this
Ad = double(A); % cast the int to a double
data = [Ad B]';
fprintf(id,'%6.0f %f\n',data); % but truncate it so it LOOKS like an int
which appears in the file like ints and floats in two columns as I wanted.
Dheeraj Maurya
Dheeraj Maurya 2022년 5월 23일

0 개 추천

what is the type =is integer(un)

댓글 수: 1

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"

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

카테고리

질문:

2016년 10월 4일

댓글:

2022년 5월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by