Error in fprintf function

1 visualización (últimos 30 días)
jonathan lochner
jonathan lochner el 7 de Jun. de 2019
Comentada: Stephen23 el 13 de Jun. de 2019
I have this line of code and it keeps saying there is an error using fprintf. I am trying to save data as a .dat file and every time I hit save this pops up. Can anyone help me understand why this is happening?
% save variable as text
fid = fopen(save_name,'w');
for i = 1:size(save_data,1);
fprintf(fid,'%15d%15.3f%15.3f%15.6f%15.6f\n',...
save_data(i,1), save_data(i,2), save_data(i,3),...
save_data(i,4), save_data(i,5));
end
fclose(fid);
  1 comentario
Steven Lord
Steven Lord el 7 de Jun. de 2019
Show us the full and exact text of the error message you receive (all the text displayed in red) and we may be able to determine what's causing it and how to modify your code so it no longer throws that error.
If my suspicion is correct you should also show us the size and class of the variable named save_data.

Iniciar sesión para comentar.

Respuesta aceptada

per isakson
per isakson el 7 de Jun. de 2019
"Invalid file identifier. Use fopen to generate a valid file identifier". The problem is most likely with this line
fid = fopen(save_name,'w');
Replace the fopen-line by
[fid,msg] = fopen(save_name,'w');
assert( fid >= 3, 'abc:def', 'Cannot open/create %s. Message: %s', save_name, msg )
and try to run the thus modified script of your question
  4 comentarios
per isakson
per isakson el 13 de Jun. de 2019
You don't answer the questions of my previous comment. Thus, I guess that the problem of your original question is solved.
Now you introduce a new problem together with a dump of 809 lines of code (including comments). I assume, I cannot run this code because vital code is missing.
As far as I can see your first error messages
Error: File: saveResults.m Line: 356 Column: 9
Illegal use of reserved keyword "case".
is not thrown by code of the dump.
See:
Stephen23
Stephen23 el 13 de Jun. de 2019
@jonathan lochner: please stop pasting 1000 lines of code and making the thread totally unreadable. You can simply upload the code by clicking the paperclip button (which is what I did for you today, and you can do in future).

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

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

Productos


Versión

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by