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

Problem with defining a matrix of variables

Asked by Amelia on 18 Jun 2013

Hi! I'm working on guide and this is the problem I have defined a variable p, that takes the number the user puts in an edit box (edit1), so I can use it later to plot something This is how I did it

__  _function edit1_Callback(hObject, eventdata, handles)
  global p;
  p = get(hObject, 'String');___

Later I use p to plot something in a graph (axes3) This is how I did it, so when I click a button (pushbutton2)it graphs my function

    function pushbutton2_Callback(hObject, eventdata, handles)
    axes(handles.axes3);
    global p
    t = [p, p, p, p,]
    plot(t,p)

--p in this case is 9

The problems is that when I try to run my code the next message appears in the MatLab Command Window

    t =
    999999999999
    Error using plot
    Invalid first data argument
    Error in vaquita>pushbutton2_Callback (line 152)
    plot(t,p)
    Error in gui_mainfcn (line 96)
            feval(varargin{:});
    Error in vaquita (line 42)
        gui_mainfcn(gui_State, varargin{:});
    Error in
    @(hObject,eventdata)vaquita('pushbutton2_Callback',hObject,eventdata,guidata(hObject))
    Error while evaluating uicontrol Callback

I don't know why, but MatLab won't recognize the variables p in the matrix t as individual elements of the t matrix

According to me, t should be as next:

      t=[9 9 9 9 9 9]

I have tried using some constant instead of p, like writing 2 instead of p, and in those cases MatLab doesnt have any problem in recognizing them as individual elements of the t matrix.

Of course, I can't use this since my program its supposed to change its plot with every number the user writes in the edit box.

I've tried commas, spaces, a lot of things, so a little help would be greatly appreciated. Thanks for your time

0 Comments

Amelia

Products

No products are associated with this question.

2 Answers

Answer by Iain on 18 Jun 2013
Accepted answer

That error is because you have assigned a STRING to p. Assign it with.

 p = str2double(get(hObject, 'String'))

1 Comment

Amelia on 18 Jun 2013

Thanks! It worked perfectly! :D you just saved my program!

Iain
Answer by Jan Simon on 18 Jun 2013

The well known problem of global variables is that it is hard to find out, who is responsible for the last change. Therefore many people recommend not to use globals, but store values locally by e.g. guidata().

This should be cause error due to the unmatched comma:

t = [p, p, p, p,]

The appearence of "t = 999999999999" cannot be understood by seeing the posted code. I suggest to use the debugger to find out, what's going on.

1 Comment

Amelia on 18 Jun 2013

thanks for your help :)

Jan Simon

Contact us