Entering a whole word into an arary.
古いコメントを表示
Hello I just have a simple question. I'm working on a project that calculates the resistor value when the user enters a five color code. I cannot figure out how to put each word as a single element in an array. so if I enter the code as a single string: blue black green red orange, I would like to make a(1)=blue, a(2)=black, etc..
Thanks in advance!
採用された回答
その他の回答 (4 件)
Azzi Abdelmalek
2012 年 11 月 5 日
編集済み: Azzi Abdelmalek
2012 年 11 月 5 日
a={'blue' , 'red', 'green'}
a(1)
a(2)
%or
a=struct('color',{'red','blue','green'})
a(1).color
a(2).color
10 件のコメント
Walter Roberson
2012 年 11 月 5 日
No, then a(1) would be the cell array {'blue'} instead of the string 'blue'
Azzi Abdelmalek
2012 年 11 月 5 日
編集済み: Azzi Abdelmalek
2012 年 11 月 5 日
for the first code : use a{1} instead of a(1)
Mark Grano
2012 年 11 月 5 日
Azzi Abdelmalek
2012 年 11 月 5 日
what do you mean?
Mark Grano
2012 年 11 月 6 日
Azzi Abdelmalek
2012 年 11 月 6 日
編集済み: Azzi Abdelmalek
2012 年 11 月 6 日
a={'blue' , 'red', 'green'}
idx = listdlg('PromptString','Select a color:',...
'SelectionMode','single','ListString',a)
color=a{idx}
Mark Grano
2012 年 11 月 8 日
Walter Roberson
2012 年 11 月 8 日
You would have to edit listdlg() so much it became unrecognizable.
Instead, create three uicontrol() of style 'listbox'.
Mark Grano
2012 年 11 月 9 日
Walter Roberson
2012 年 11 月 9 日
You can create a figure() that you have the uicontrol() in. listdlg() creates a figure and appropriate controls for its operations; the only difference is that it is already written for you.
Walter Roberson
2012 年 11 月 5 日
編集済み: Walter Roberson
2012 年 11 月 5 日
0 投票
Not possible. See http://www.mathworks.co.uk/matlabcentral/answers/52631-create-an-array-contain-no-of-string-adding-one-by-one#answer_64157 for work-around
Image Analyst
2012 年 11 月 5 日
>> theWords = allwords('blue black green red orange')
theWords =
'blue' 'black' 'green' 'red' 'orange'
theWords is a cell array because the words can have different lengths so it cannot be a rectangular character array.
Walter Roberson
2012 年 11 月 6 日
Similar to strtok() is to use regexp() with the 'split' option. After getting the input from the user (e.g., questdlg() or a uicontrol() editbox),
theWords = regexp(S, 'split');
Just as with allwords(), theWords will be a cell array.
Note that theWords(1) would be a cell array containing a string, rather than the string itself. theWords{1} would be the string.
カテゴリ
ヘルプ センター および File Exchange で Data Type Conversion についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!