Error in fprintf function
조회 수: 1 (최근 30일)
이전 댓글 표시
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
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
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
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
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개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!