How do I write a code to verify that the filename entered by the user ends in .txt?

2 ビュー (過去 30 日間)
Elaine Collins
Elaine Collins 2018 年 11 月 28 日
コメント済み: Greg 2018 年 11 月 28 日
This is what I have so far.
filename = input('Enter your filename ending in .txt: ', 's');
s1 = '.txt';
s2 == 'filename'
tf == strcmp(s1,s2)
if tf == 0
filename = input('Enter your filename ending in .txt: ', 's');
end
Thanks!

採用された回答

Greg
Greg 2018 年 11 月 28 日
編集済み: Greg 2018 年 11 月 28 日
filename = input('Enter your filename ending in .txt: ', 's');
s1 = '.txt';
s2 == 'filename' % <-- I think you're confused on = versus == here
tf == strcmp(s1,s2) % <-- and here
if tf == 0
filename = input('Enter your filename ending in .txt: ', 's');
end
You can achieve this quite simply with:
filename = input(___);
while ~endsWith(filename,".txt")
filename = input(___);
end
endsWith was introduced in R2016b, and double quotes to create strings in R2017a.
  2 件のコメント
Elaine Collins
Elaine Collins 2018 年 11 月 28 日
That was much easier, thank you!
Greg
Greg 2018 年 11 月 28 日
Happy to help!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeWhos についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by