display file function crash
4 ビュー (過去 30 日間)
古いコメントを表示
%I want to display a file and according to the book, I wrote the display file function like below, but it always showed that 'something wrong happen'. I have no idea which I part I did the wrong programming.%
function DisplayFile(filename)
try
fid=fopen(filename);
if fid == -1
disp(sprintf('Unable to open file ''%s'' ',filename));
else
while ~feof(fid)
tline = fget1(fid);
disp(tline);
end
fclose(fid);
end
catch
fclose(fid);
disp('Something wrong happened');
end
2 件のコメント
Steven Lord
2019 年 10 月 21 日
Show us exactly how you call this function. What are you passing into it as input?
回答 (1 件)
Daniel M
2019 年 10 月 20 日
編集済み: Daniel M
2019 年 10 月 20 日
try
% some code here
x = 1:5;
for n = 0:4
y = x(n); % can't access x at position zero!
end
catch ex
% some code to handle it
errorlog = n;
rethrow(ex) % throw the error to view message
end
2 件のコメント
Daniel M
2019 年 10 月 21 日
If you run the above code in a script the error it throws should be:
Array indices must be positive integers or logical values.
Error in untitled27 (line 5)
y = x(n); % can't access x at position zero!
This tells you exactly what part goes wrong and where.
参考
カテゴリ
Help Center および File Exchange で Programming Utilities についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!