Error in fprintf function

2 visualizzazioni (ultimi 30 giorni)
jonathan lochner
jonathan lochner il 7 Giu 2019
Commentato: Stephen23 il 13 Giu 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 Commento
Steven Lord
Steven Lord il 7 Giu 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.

Accedi per commentare.

Risposta accettata

per isakson
per isakson il 7 Giu 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 Commenti
per isakson
per isakson il 13 Giu 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 il 13 Giu 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).

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su MATLAB in Help Center e File Exchange

Prodotti


Release

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by