I want to solve following question
Write a function that will prompt the user for a string of at least one character, loop to error-check to make sure that the string has at least one character, and return the string?
I has written the follwing code
function outstring=outstrng()
%return string
outstring=input('Enter the string: ','s');
stg=char(outstring);
while outstring~=stg
outstring=input('Invalid! Enter string only: ','s');
stg=char(outstring);
end
end
But the above scipt is not working.Please correct my code.

2 件のコメント

Geoff Hayes
Geoff Hayes 2019 年 5 月 23 日
Arpit - the question is asking you to "loop to error-check to make sure that the string has at least one character,". So that is your condition - if the string has at least one character (i.e. the length of the string is at least one) then return the string. You don't need to convert it to a char...just check the length. Unless you need to check to see if the string contains at least one non-numeric character?
You may also want to rename your function or rename your output parameter as they are near identical and can lead to confusion.
Arpit Jain
Arpit Jain 2019 年 5 月 23 日
編集済み: Arpit Jain 2019 年 5 月 23 日
@Geoff Hayes Thansk for describing

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

 採用された回答

James Tursa
James Tursa 2019 年 5 月 23 日

0 投票

From your description, it seems like you simply need to test for the number of characters. E.g.,
while numel(outstring) == 0

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMatrices and Arrays についてさらに検索

タグ

質問済み:

2019 年 5 月 23 日

編集済み:

2019 年 5 月 23 日

Community Treasure Hunt

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

Start Hunting!

Translated by