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

3 vues (au cours des 30 derniers jours)
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!

Réponse acceptée

Greg
Greg le 28 Nov 2018
Modifié(e) : Greg le 28 Nov 2018
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.

Plus de réponses (0)

Catégories

En savoir plus sur Numeric Types dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by