Converting user input char to an array

As part of my university matlab module i was assigned to do an assigment from one of 4 options. I'm on a bit of a road block, part of code is requesting the user to type in 6 numbers for a lottery game, being that this data is recorded as a char i want to convert this to a numerial array (matrix). I have tried the following with no luck.
player_numbers = input ('Please enter your number: ','s');
player_lotto_numbers = str2num(player_numbers);

2 件のコメント

Walter Roberson
Walter Roberson 2019 年 4 月 20 日
That code should work for a variety of input formats, including if the user puts spaces or commas between the numbers. What problem are you encountering?
Mehmet Saglam
Mehmet Saglam 2019 年 4 月 21 日
It comes back with the following error "undefined function 'str2sum' for input arguments of type 'char'". I have search everywhere for an answer but I have found nothing

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

回答 (1 件)

madhan ravi
madhan ravi 2019 年 4 月 21 日
編集済み: madhan ravi 2019 年 4 月 21 日

0 投票

It‘s not st2sum it’s str2num(). Usually str2double() is preferred.

8 件のコメント

madhan ravi
madhan ravi 2019 年 4 月 21 日
Also if 's' is removed from input() the input is assumed as a double array but to be kept in mind that the input is a valid input for instance
[1,4,6]
Walter Roberson
Walter Roberson 2019 年 4 月 21 日
As the user is entering multiple numbers, str2double() would not handle the situation directly.
madhan ravi
madhan ravi 2019 年 4 月 21 日
Agree, sir Walter.
Stephen23
Stephen23 2019 年 4 月 21 日
"It‘s not st2sum it’s strnum()"
Actually its str2num, although str2double is generally preferred.
It is often possible to use sscanf for multiple numbers in an vector.
madhan ravi
madhan ravi 2019 年 4 月 21 日
Thank you Stephen indeed it’s str2num() it was a typo.
Mehmet Saglam
Mehmet Saglam 2019 年 4 月 21 日
I'm a bit of a novice, soo how would I approach this problem. Would stake the 's' solve the problem I'm having?
Walter Roberson
Walter Roberson 2019 年 4 月 21 日
What you posted is str2num(), which is valid.
But your error message was about str2sum() with a 'sum' instead of a 'num' . That does not exist.
The code you had posted was right.

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

カテゴリ

ヘルプ センター および File ExchangeData Type Conversion についてさらに検索

質問済み:

2019 年 4 月 20 日

コメント済み:

2019 年 4 月 21 日

Community Treasure Hunt

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

Start Hunting!

Translated by