MATLAB Answers

Preventing text input for a variable

3 ビュー (過去 30 日間)
Samuel Brewton
Samuel Brewton 2021 年 2 月 16 日
コメント済み: Samuel Brewton 2021 年 2 月 19 日
I am working on a function where a user with no prior coding experience should be able to input a series of numbers to define my variables. The variables must be between 1 and 30 and must be an integer.
I currently have preventions to stop numbers outside of 1 and 30 (using logic) and another to stop decimals (using round(Var)).
The last prevention I am looking for is stopping the user from inputing words or letters. Ex: Var=q
I appreciate any help.
Fxn(20) %Input must be between 1 and 30; cannot be a word or letter
function output=Fxn(x)
x=round(x)
if x<1 | x>30
disp("Variable must be between 1 and 30")
return
else
Matrix=zeros(1,x)
end
end
  1 件のコメント
Rik
Rik 2021 年 2 月 16 日
Should the user be able to input the series of numbers in one go? Or should they always provide a scalar?
How are you getting your data from the user?
You don't define your output variable in your function, it doesn't deal with array inputs, and it is missing documentation.

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

採用された回答

Jemima Pulipati
Jemima Pulipati 2021 年 2 月 19 日
Hello,
From my understanding, you want to check if the given input is a string or not.
The isstring() is useful to determine if input is string array.
The ischar() is useful to determine if input is char array.
You may consider using these functions in your code.
  1 件のコメント
Samuel Brewton
Samuel Brewton 2021 年 2 月 19 日
This works, thank you!

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

その他の回答 (0 件)

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by