MATLAB Answers


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)
    global p
    t = [p, p, p, 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 =
    Error using plot
    Invalid first data argument
    Error in vaquita>pushbutton2_Callback (line 152)
    Error in gui_mainfcn (line 96)
    Error in vaquita (line 42)
        gui_mainfcn(gui_State, varargin{:});
    Error in
    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


Log in to comment.

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

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

Log in to comment.

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

thanks for your help :)

Log in to comment.

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

MATLAB Academy

New to MATLAB?

Learn MATLAB today!