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
stozaki 2020 年 3 月 15 日
Your while loop has the wrong syntax.
Define a condition equivalent to expression.
while expression
statements
end
Walter Roberson
Walter Roberson 2020 年 3 月 15 日
You should use the 's' option on input()
And see exist()
Pallav Patel
Pallav Patel 2020 年 3 月 15 日
編集済み: Pallav Patel 2020 年 3 月 15 日
Thank you Robertson. That is actually a rough view of what I am trying to create. The input that the user types is an excel file name that will later be imported using the xlsread() function. If MATLAB does not recognise the file within it directory it usually brings an error. I would like to create a loop for that error to be supressed and instead the loop to be brought up to ask the user for another input incase the user messed the spelling or case. Could you please elaborate on how to use the exist function. I think it would come in handy.
Walter Roberson
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
Pallav Patel 2020 年 3 月 15 日
編集済み: Pallav Patel 2020 年 3 月 15 日
clc
clear
close all
%Input for data file name
myFilename = input('Please enter data file name:');
myFilename = '';
while ~exist(myFilename, 'MP_JIT_data.xlsx')
myFilename = input('Please enter the correct data file name(Case sensitive). Ensure spelling is correct:', 's');
end
%Import data from excel file
[times,Company_name,AllData] = xlsread(myFilename)
I am trying it but I keep on getting an error. Is there anything I am doing wrong. I modified some elements as replacing it directly didn't work
Walter Roberson
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
Pallav Patel 2020 年 3 月 15 日
Wow thank you. That works perfectly. Is file a function?
Walter Roberson
Walter Roberson 2020 年 3 月 15 日

サインインしてコメントする。

回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeData Import from MATLAB についてさらに検索

タグ

質問済み:

2020 年 3 月 15 日

コメント済み:

2020 年 3 月 15 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by