str2num not working on array

8 ビュー (過去 30 日間)
Nikolay N Valov
Nikolay N Valov 2019 年 9 月 18 日
編集済み: Stephen23 2019 年 9 月 19 日
So the main issue is that when I used inputdlg and put in my desired color, str2num does not read it. I have tried str2double which produces a NaN which makes sense. I am not sure how to circumvent this issue.
%initialize colors
Grey = [0.5 0.5 0.5];
Red = [1 0 0];
Blue = [ 0 1 0];
Green = [ 0 0 1];
%prompt details of simulation
screenXpixels = 1920;
screenYpixels = 1080;
baseRect = [0 0 200 200];
prompt = {'Enter Arc Color (Grey, Red, Green, Blue):','Enter Arc Radius:','Enter Arc Angle','Enter amplitude constant:','Enter Frequency of Oscillation'};
dlgtitle = 'OscillatingDotDemo';
answer = inputdlg(prompt,dlgtitle);
arcColor = str2num(answer{1,1});
...
So when I run this, it returns an empty array.
  1 件のコメント
Adam
Adam 2019 年 9 月 18 日
str2num will convert e.g. '7' (char) to 7 (double), it doesn't have magic properties to interpret anything beyond that though, such as converting a colour string to a colour rgb or the index into a list of colour strings.

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

回答 (2 件)

Star Strider
Star Strider 2019 年 9 月 18 日
It returns an empty array because you apparently ask for a character array (for example ‘Red’) as the first input. That is not a number. If you enter a number instead of a color, it will return that number.

Stephen23
Stephen23 2019 年 9 月 19 日
編集済み: Stephen23 2019 年 9 月 19 日
It would be simpler to use a structure (in this example I entered "Red" into the dialog box):
>> S.red = [1,0,0];
>> S.green = [0,1,0];
>> S.blue = [0,0,1];
>> S.gray = [1,1,1]*0.5;
>> S.(lower(answer{1}))
ans =
1 0 0

カテゴリ

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

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by