Error in fprintf function

조회 수: 1 (최근 30일)
jonathan lochner
jonathan lochner 2019년 6월 7일
댓글: Stephen23 2019년 6월 13일
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
Steven Lord
Steven Lord 2019년 6월 7일
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.

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

채택된 답변

per isakson
per isakson 2019년 6월 7일
"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
per isakson
per isakson 2019년 6월 13일
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 2019년 6월 13일
@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).

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by