I want my function to return -1 when the file doesn't open.
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/950719/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/950724/image.png)
0 Commenti
Risposta accettata
Voss
il 2 Apr 2022
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.
2 Commenti
Più risposte (1)
Riccardo Scorretti
il 2 Apr 2022
Modificato: Riccardo Scorretti
il 2 Apr 2022
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
Vedere anche
Categorie
Scopri di più su Data Import and Export in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!