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!

 채택된 답변

Matt Fig
Matt Fig 2012년 11월 8일
편집: Matt Fig 2012년 11월 9일

1 개 추천

This works whether your user enters only commas between words, only spaces, or both (even colons or semicolons!). I test it out in a FOR loop only to see if entering different ways people might use will still get us our values.
for ii = 1:4
% You only need these three lines, no loop.
I = input('Enter the colors you want: ','s');
I(isstrprop(I,'punct')) = ' ';
C = regexp(I(~isstrprop(I,'punct')),'\s+','split')
end
Enter the colors you want: black gold green
C =
'black' 'gold' 'green'
Enter the colors you want: yellow, peach, orange
C =
'yellow' 'peach' 'orange'
Enter the colors you want: purple:spicy:fresh
C =
'purple' 'spicy' 'fresh'
Enter the colors you want: Yummy; grape;cherry
C =
'Yummy' 'grape' 'cherry'

댓글 수: 6

Mark Grano
Mark Grano 2012년 11월 9일
this could work also, but is there a way I could access each word? say if I entered: black green orange the disp(c(1));
it would say black?
Matt Fig
Matt Fig 2012년 11월 9일
편집: Matt Fig 2012년 11월 9일
C is a cell array. To access the elements, use cell indexing...
>>I = input('Enter the colors you want: ','s');
I(isstrprop(I,'punct')) = ' ';
C = regexp(I(~isstrprop(I,'punct')),'\s+','split')
Enter the colors you want: Yellow, green,purple black
C =
'Yellow' 'green' 'purple' 'black'
>> C{3} % Note {} not (). Could also: disp(C{2})
ans =
purple
Mark Grano
Mark Grano 2012년 11월 9일
thats exactly what I needed! Thanks!
and one more quick question if you don't mind. How would I make all the letters lowercase? so that blue,Blue,BLUE, etc.. can all be entered and a valid comparison to 'blue'
Image Analyst
Image Analyst 2012년 11월 9일
Use the lower() function.
>> lower('ABC')
ans =
abc
Mark Grano
Mark Grano 2012년 11월 9일
perfect thanks for your help!

댓글을 달려면 로그인하십시오.

추가 답변 (4개)

Azzi Abdelmalek
Azzi Abdelmalek 2012년 11월 5일
편집: Azzi Abdelmalek 2012년 11월 5일

0 개 추천

a={'blue' , 'red', 'green'}
a(1)
a(2)
%or
a=struct('color',{'red','blue','green'})
a(1).color
a(2).color

댓글 수: 10

Walter Roberson
Walter Roberson 2012년 11월 5일
No, then a(1) would be the cell array {'blue'} instead of the string 'blue'
Azzi Abdelmalek
Azzi Abdelmalek 2012년 11월 5일
편집: Azzi Abdelmalek 2012년 11월 5일
for the first code : use a{1} instead of a(1)
Mark Grano
Mark Grano 2012년 11월 5일
This does what I need, but how would I assign the elements through user input? and all at once without having to press enter after each word.
Azzi Abdelmalek
Azzi Abdelmalek 2012년 11월 5일
what do you mean?
Mark Grano
Mark Grano 2012년 11월 6일
well the user should input: black red blue orange yellow
this is all at once. then black should be assigned a(1) and so on. but user inputs can obviously change from run to run so I can't just manually assign a(1) to black.
Azzi Abdelmalek
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
Mark Grano 2012년 11월 8일
i like this solution and it has worked very well for me. However is there any way to make a listdlg that has multiple options? for example there would be a list for the first band, second band, and third band, then you press ok, and all are assigned to say color1, color2, and color3 respectively? If you could do this that would make my program much cleaner. thank you!
Walter Roberson
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
Mark Grano 2012년 11월 9일
hmm, I can see how that would get complicated. I just wanted a way instead of having 6 different dialog boxes, to just have one so I could enter all the colors in at once.
Walter Roberson
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.

댓글을 달려면 로그인하십시오.

Image Analyst
Image Analyst 2012년 11월 5일

0 개 추천

Maybe you'd like to use the built-in strtok(). I prefer allwords:
>> 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
Walter Roberson 2012년 11월 6일

0 개 추천

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에서 Structures에 대해 자세히 알아보기

질문:

2012년 11월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by