필터 지우기
필터 지우기

Problem with defining a matrix of variables

조회 수: 1 (최근 30일)
Amelia
Amelia 2013년 6월 18일
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

채택된 답변

Iain
Iain 2013년 6월 18일
That error is because you have assigned a STRING to p. Assign it with.
p = str2double(get(hObject, 'String'))
  댓글 수: 1
Amelia
Amelia 2013년 6월 18일
Thanks! It worked perfectly! :D you just saved my program!

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

추가 답변 (1개)

Jan
Jan 2013년 6월 18일
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.

카테고리

Help CenterFile Exchange에서 Graphics Object Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by