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

convert popupmenu's selected string to number form

Asked by pammy on 29 Jun 2013

i've popupmenu having entries as

 2
 4
 8
 16
 32
 64
 128

i want when user selects '2' , this string value convert to 2 as number so that i can use it in variable and i can use it in my mathematical calculations.

i tried following code....

 str = get(handles.popupmenu3,'String');
 val = get(handles.popupmenu3,'Value');
 switch str{val};
    case '2'
          a = get(handles.popupmenu3,'String');
          a1 = str2num(a);
          b = get(handles.popupmenu3,'String');
          b1 = str2num(b);
    case '4'
          a = get(handles.popupmenu3,'String');
          a1 = str2num(a);
          b = get(handles.popupmenu3,'String');
          b1 = str2num(b);
   case '8'
         a = get(handles.popupmenu3,'String');
          a1 = str2num(a);
          b = get(handles.popupmenu3,'String');
          b1= str2num(b);
  case '16' 
         a = get(handles.popupmenu3,'String');
          a1 = str2num(a);
          b = get(handles.popupmenu3,'String');
          b1 = str2num(b);
  case '32'
         a = get(handles.popupmenu3,'String');
          a1 = str2num(a);
          b = get(handles.popupmenu3,'String');
          b1 = str2num(b);
  end

same for other cases like 64 and 128...

when i run this i got the error:

Error using str2num (line 33)
Requires string or character array input.

plz help me in correcting my error...

0 Comments

pammy

Products

No products are associated with this question.

1 Answer

Answer by Walter Roberson on 29 Jun 2013
Accepted answer

You don't need the switch and cases. Just use

a1 = str2double(str{val});
b1 = a1;

0 Comments

Walter Roberson

Contact us