Loop for repeated file input if error
이전 댓글 표시
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
stozaki
2020년 3월 15일
Your while loop has the wrong syntax.
Define a condition equivalent to expression.
while expression
statements
end
Walter Roberson
2020년 3월 15일
You should use the 's' option on input()
And see exist()
Pallav Patel
2020년 3월 15일
편집: Pallav Patel
2020년 3월 15일
Walter Roberson
2020년 3월 15일
myFilename = '';
while ~exist(myFilename, 'file')
myFilename = input('Please enter the correct data file name(Case sensitive). Ensure spelling is correct:', 's');
end
Pallav Patel
2020년 3월 15일
편집: Pallav Patel
2020년 3월 15일
Walter Roberson
2020년 3월 15일
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
2020년 3월 15일
Walter Roberson
2020년 3월 15일
No, 'file' is an option name
답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Data Import and Analysis에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!