Problems with fprintf format (Matlab)

조회 수: 6 (최근 30일)
Isaac Hormigo
Isaac Hormigo 2020년 12월 20일
댓글: Star Strider 2020년 12월 24일
I want to correct variables' format in a txt file, using the next code (previous import as separated arrays: skipping the first row (with text headers), defining date as a string):
id = fopen('data.txt', 'w');
fprintf(id, '%5s %3s %3s %3s %4s %3s %6s\n',...
'fecha', 'dia','mes', 'ano', 'hora', 'min', 'abs370');
datos = cat(2,dia, mes, ano, hora, min1, abs370);
datos = datos';
fecha = Fecha'; % Imported as a string
fprintf(id, '%16s %2i %2i %4i %2i %2i %8.4f\n',...
fecha, datos);
fclose(id);
type data.txt
But I get this error:
Error using fprintf Unable to convert 'string' value to 'int64'.
Why do I get this error?

답변 (1개)

Star Strider
Star Strider 2020년 12월 20일
You have R2020a.
Use writetable instead.
  댓글 수: 2
Isaac Hormigo
Isaac Hormigo 2020년 12월 24일
편집: Isaac Hormigo 2020년 12월 24일
Ok, thanks. But why do I get this error? Is it not posible to mix both format in this way?
Star Strider
Star Strider 2020년 12월 24일
My pleasure!
No.
Some file formats will allow that, the reason I suggest writetable. It will do all that for you.
Also, if you want to import everything, readtable is the best option. (The documentation for it is linked at the end of the writetable documentation page.)

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

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by