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
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