An error of using fprintf

33 visualizaciones (últimos 30 días)
Benson Gou
Benson Gou el 15 de Sept. de 2020
Comentada: Star Strider el 15 de Sept. de 2020
Dear All,
When I tried to use fprintf to print numerical data and char in the same row, here is my code:
fprintf(fileID, ' %7d %8d %6d %s %4.2f %s %4.2f %3.1f\r\n', [d1 d2 d3 d4' d5 d6' d7 d8]');
Unfortunately, I obtained the following error message:
Error using fprintf
Unable to convert 'string' value to 'int64'.
Would anyone could tell me how to fix this error?
Thanks a lot.
Benson

Respuesta aceptada

Star Strider
Star Strider el 15 de Sept. de 2020
Printing different variable classes (such as numeric and character), requires a loop:
for k = 1 : something
fprintf(fileID, ' %7d %8d %6d %s %4.2f %s %4.2f %3.1f\r\n', [d1(k) d2(k) d3(k) d4(k) d5(k) d6(k) d7(k) d8(k)]);
end
fclose(fileID);
... or something similar.
Experiment to get the desired result
  2 comentarios
Benson Gou
Benson Gou el 15 de Sept. de 2020
Using loop is not the cause of the problem. I tried to use loop, I still encountered the same problem.
By the way, using loop is not necessary to print more than 2 rows.
Thanks a lot, anyway.
Benson
Star Strider
Star Strider el 15 de Sept. de 2020
As always, my pleasure!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre String Parsing en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by