I want my function to return -1 when the file doesn't open.
1 次查看(过去 30 天)
显示 更早的评论
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/950719/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/950724/image.png)
0 个评论
采纳的回答
Voss
2022-4-2
Your code is explicitly throwing the error, here:
error('error opening file %s\n',fname);
You can remove that line (and the next one because there is no need to close a file that's not open), so that that part looks like this:
if fid < 0
charnum = -1;
return
end
Then the function will return -1 in that case.
更多回答(1 个)
Riccardo Scorretti
2022-4-2
编辑:Riccardo Scorretti
2022-4-2
Hi. You must return just after assigning the value -1 to charnum. If you call the function error... well, you get an error message and the program stops. Use warning instead.
if fid == -1
charnum = -1;
warning('error opening file %s', fname);
return
end
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Import and Export 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!