Script to read, error check till user enters a valid file name that can be opened, then print out file name and close file.
8 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Nora
am 25 Okt. 2013
Kommentiert: Nora
am 25 Okt. 2013
I am having the hardest trouble on the loop error-checking part that keeps checking still the user enters a valid filename that can be opened.
This is what I have so far:
% prompt user for name to be read and open
file = input('PLEASE INPUT FILE NAME TO BE READ:','s');
fopen(file,'r');
% loop error-check until valid filename can be opened
% print file name and close file
while fopen(file)~=0
fpirnt('FILE OPENED IS: %s\n',file);
fclose(file);
else
fprintf('PLEASE INPUT A VALID FILE NAME!\n','s');
file = input('PLEASE INPUT FILE NAME TO BE READ:','s');
end
what is going wrong?
Thank you!
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 25 Okt. 2013
'while' does not permit an 'else', so you have a syntax error.
You cannot close a file by name, only by file identifier.
Hint: "break"
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Convert Image Type finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!