How to have MatLab put input numbers into an array?

4 ビュー (過去 30 日間)
Rachel Frings
Rachel Frings 2015 年 2 月 3 日
編集済み: Image Analyst 2015 年 2 月 3 日
I need help putting numbers into an array. I have a problem where the user inputs six numbers and I want the program to ouput them in into an array. For example, number=('What is your number') Then the user types in 54-134-1-23-6-41 I want the program to come back with [54, 134, 1, 23, 6, 41] Instead, it acts as though the '-' is a subtraction function. Anyway to have it recognize it as separating numbers in the array?

回答 (1 件)

dpb
dpb 2015 年 2 月 3 日
編集済み: Image Analyst 2015 年 2 月 3 日
You'll have to read the returned input as a string rather than an expression which is the default behavior of input
number=input('What are your numbers:', 's');
values = cell2mat(textscan(number, '%d', 'delimiter', '-'))
It's a very "user-belligerent" interface in my judgement, however, in expecting the user to format such and not make any mistakes.

カテゴリ

Help Center および File ExchangeData Types についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by