Loop for repeated file input if error
Ältere Kommentare anzeigen
clc
clear
close all
%Input for data file name
myFilename = input('Please enter data file name.(Case sensitive):''s');
myFilename1 = 'myFilename';
%While loop if datafile does not exist
while <filenamedoesnotexist>
myFilename = input('Please enter the correct data file name(Case sensitive). Ensure spelling is correct:');
myFilename1 = 'myFilename';
end
%Import data from excel file
[times,Company_name,~] = xlsread(myFilename1)
I want Matlab to create a while loop if the file name entered brings in an error like file does not exist until user writes correct file name.
8 Kommentare
stozaki
am 15 Mär. 2020
Your while loop has the wrong syntax.
Define a condition equivalent to expression.
while expression
statements
end
Walter Roberson
am 15 Mär. 2020
You should use the 's' option on input()
And see exist()
Pallav Patel
am 15 Mär. 2020
Bearbeitet: Pallav Patel
am 15 Mär. 2020
Walter Roberson
am 15 Mär. 2020
myFilename = '';
while ~exist(myFilename, 'file')
myFilename = input('Please enter the correct data file name(Case sensitive). Ensure spelling is correct:', 's');
end
Pallav Patel
am 15 Mär. 2020
Bearbeitet: Pallav Patel
am 15 Mär. 2020
Walter Roberson
am 15 Mär. 2020
In the syntax
while ~exist(myFilename, 'file')
the 'file' part is the literal keyword 'file' and is not to be replaced with a file name.
myFilename = input('Please enter data file name:', 's');
while ~exist(myFilename, 'file')
myFilename = input('Please enter the correct data file name (Case sensitive). Ensure spelling is correct:', 's');
end
Pallav Patel
am 15 Mär. 2020
Walter Roberson
am 15 Mär. 2020
No, 'file' is an option name
Antworten (0)
Kategorien
Mehr zu Data Import and Analysis finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!