フィルターのクリア

Unexpected MATLAB operator during using isnumeric and isletter

2 ビュー (過去 30 日間)
jarvan
jarvan 2014 年 11 月 18 日
コメント済み: Orion 2014 年 11 月 19 日
I am writing a function that prompts the user for a number immediately followed by a letter of the alphabet. The format should be number first then letter after. I got error when I called the function up , I typed 123d and it said Unexpected MATLAB operator.
Here is the example:
Enter a letter and a #: xyz4.5t
Error!
Enter a letter and a #: 3.21f
num =
3.2100
let =
f
And here is my code
function[num let] = readthem(word)
fprintf('Please enter a number immediately followed by a letter of the alphabet \n')
word = input('Enter a letter and a #: ')
while isnumeric(num)==0 || isletter('let')== 0
word = input('Enter a letter and a #: ')
end
sprintf('You enter %d%s',num,let)
end

採用された回答

Orion
Orion 2014 年 11 月 18 日
編集済み: Orion 2014 年 11 月 18 日
Jarvan,
you misused the call to input
when you use
word = input('Enter a letter and a #: ')
Matlab expects the user to give a valid expression, not a string.
what you want to do is to give a string as input, so, just do
word = input('Enter a letter and a #: ','s') % add a second argument to _input_ to consider the input as a string.
and then you can decompose the data word in two parts num and let .
Note : in your code, num and let are not calculated ?!
  3 件のコメント
jarvan
jarvan 2014 年 11 月 19 日
I also know I can use [num let] = strtok to display what user just input, however, I don't know where should I put during using function
Orion
Orion 2014 年 11 月 19 日
in a simplier way
function [num,let] = readthem()
fprintf('Please enter a number immediately followed by a letter of the alphabet \n');
word = input('Enter a letter and a #: ','s');
let = word(isletter(word));
num = word(~isletter(word));
fprintf('You entered %s %s',num,let);
end

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by