Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Entering a whole word into an arary.

Asked by Mark Grano on 5 Nov 2012

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!

0 Comments

Mark Grano

Products

No products are associated with this question.

6 Answers

Answer by Matt Fig on 8 Nov 2012
Edited by Matt Fig on 9 Nov 2012
Accepted answer

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 Comments

Image Analyst on 9 Nov 2012

Use the lower() function.

per isakson on 9 Nov 2012
>> lower('ABC')
ans =
abc
Mark Grano on 9 Nov 2012

perfect thanks for your help!

Matt Fig
Answer by Azzi Abdelmalek on 5 Nov 2012
Edited by Azzi Abdelmalek on 5 Nov 2012
 a={'blue' , 'red', 'green'}
 a(1)
 a(2)

%or

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

10 Comments

Walter Roberson on 8 Nov 2012

You would have to edit listdlg() so much it became unrecognizable.

Instead, create three uicontrol() of style 'listbox'.

Mark Grano on 9 Nov 2012

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 on 9 Nov 2012

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.

Azzi Abdelmalek
Answer by Image Analyst on 5 Nov 2012

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.

0 Comments

Image Analyst
Answer by Walter Roberson on 6 Nov 2012

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.

0 Comments

Walter Roberson
Answer by vincent on 11 Mar 2013

For some nice info on resistor color codes http://www.resistorguide.com/resistor-color-code/

0 Comments

vincent

Contact us