how do I print a txt file of integers and floats?

83 visualizaciones (últimos 30 días)
John Petersen
John Petersen el 4 de Oct. de 2016
Comentada: Walter Roberson el 24 de Mayo de 2022
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?

Respuesta aceptada

Walter Roberson
Walter Roberson el 4 de Oct. de 2016
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 comentarios
John Petersen
John Petersen el 4 de Oct. de 2016
Ahhhh, I see now. Thanks Walter, that is a great solution.
Walter Roberson
Walter Roberson el 4 de Oct. de 2016
This approach works even when some of the variables to be output are cell array of character (what used to be known as strings.)

Iniciar sesión para comentar.

Más respuestas (4)

Joe Yeh
Joe Yeh el 4 de Oct. de 2016
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 el 4 de Oct. de 2016
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 comentarios
John Petersen
John Petersen el 5 de Oct. de 2016
yes, that's exactly what happens
Walter Roberson
Walter Roberson el 5 de Oct. de 2016
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.

Iniciar sesión para comentar.


John Petersen
John Petersen el 4 de Oct. de 2016
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 el 23 de Mayo de 2022
what is the type =is integer(un)
  1 comentario
Walter Roberson
Walter Roberson el 24 de Mayo de 2022
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"

Iniciar sesión para comentar.

Categorías

Más información sobre Text Data Preparation en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by