Assigning plot to an existing axes Matlab GUI

Hi, I have a GUI in Matlab and several functions in it. One function is for plotting a figure, I need to assign it to an existing axes in GUI. I have tried several options, nothing has worked out yet.
Code below is the current option I had tried before I asked here.
set('CurrentAxes','axes11')
plot(VyQRS(:,2));
grid on
The plot of VyQRS I need to assign to axes11. Could you please give me any hint?

 Accepted Answer

Try this:
axes(handles.axes11); % Switch current axes to axes11.
plot(VyQRS(:,2)); % Will plot into axes11.
grid on

8 Comments

Didn't work out. Error:
Undefined variable "handles" or class "handles.axes10".
This is the code for the button:
% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, Data)
% hObject handle to pushbutton4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
pointsQRS = QRS_points (Data.fileData(:,1), Data.fileData(:,2),Data.fileData(:,3))
save 'pointsQRS.mat' -mat pointsQRS
Try to obtain the handles struct:
handles = guidata(hObject);
Code for the button:
% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, Data)
% hObject handle to pushbutton4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles = guidata(hObject);
pointsQRS = QRS_points (Data.fileData(:,1), Data.fileData(:,2),Data.fileData(:,3))
save 'pointsQRS.mat' -mat pointsQRS
Code of the function:
VyQRS=[[0:length(VyQRS)-1]' VyQRS];
axes(handles.axes11);
plot(VyQRS(:,2));
grid on
Vy_QRS=ginput;
y_pointsQRS=VyQRS(Vy_QRS(1,1)<=VyQRS(:,1) & Vy_QRS(2,1)>=VyQRS(:,1),:);
if size(y_pointsQRS,1)<m
m=2;
end
Error:
Undefined variable "handles" or class "handles.axes10".
What is "the function"? If it's a callback, you should already have handles in there and it should not give you that error. If you have your own function you wrote, then you'll need to pass handles into it so that it can see handles.
For the first function you show, pushbutton4_Callback(), you should have the guidata() call be the last line of code in the function, not the first.
Finally, I have no idea why the error mentions handles.axes10 since you don't even reference that axes. You only reference axes11, NOT axes 10. It leads me to believe that you're not showing me all the code. I know for a fact that you're not showing all the error message because the full error message has things like the line number and the actual line of source code that threw the error. Why you chose to just snip out a small snippet of the error message and not share the entire error message ( ALL the red text), I have no idea. Please always include the entire error message if you want help.
Full error:
Undefined variable "handles" or class "handles.axes10".
Error in QRS_points (line 6)
axes(handles.axes10);
Error in VKG_Zobrazovac>pushbutton4_Callback (line 155)
pointsQRS = QRS_points (Data.fileData(:,1), Data.fileData(:,2), Data.fileData(:,3))
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in VKG_Zobrazovac (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)VKG_Zobrazovac('pushbutton4_Callback',hObject,event data,guidata(hObject))
Error while evaluating UIControl Callback
It should be axes11, not 10, my mistake. And yes, for the push button I have written my own function. Here I reupload the function again:
VyQRS=[[0:length(VyQRS)-1]' VyQRS];
axes(handles.axes11);
plot(VyQRS(:,2));
grid on
Vy_QRS=ginput;
y_pointsQRS=VyQRS(Vy_QRS(1,1)<=VyQRS(:,1) & Vy_QRS(2,1)>=VyQRS(:,1),:);
if size(y_pointsQRS,1)<m
m=2;
end
That's how it looks now the function.
Did you understand, what the "handles" struct contain and how it is provided to other callbacks? You can use the handle of the "axes11" only, if you have stored it such, that you can you it where it is required. If the handle of teh "axes11" is stored in the handles struct, you can used it, if this struct is available, e.g. by obtaining it by guidata, get/setappdata, storing it in the UserData, etc. You find many corresponding threads in this forum. I assume, that your problem can be solved very fast, but currently there is always the one or other tiny detail missing in the explanantions. But actually it is trivial: Care to providing the required axes handle.
To fix you need to pass handles into your custom pushbutton function:
function MyCustomPushButtonFunction(handles)
VyQRS=[[0:length(VyQRS)-1]' VyQRS];
axes(handles.axes11);
plot(VyQRS(:,2));
grid on
Vy_QRS=ginput;
y_pointsQRS=VyQRS(Vy_QRS(1,1)<=VyQRS(:,1) & Vy_QRS(2,1)>=VyQRS(:,1),:);
if size(y_pointsQRS,1)<m
m=2;
end
Then, in a callback function, which automatically has access to handles, or some other function that has access to handles, you must call your custom function and pass handles to it:
MyCustomPushButtonFunction(handles); % Call your custom function.
Still does not work. Did exactly what You told me to do.
Error:
Undefined function or variable 'VxQRS'.
Error in VKG_Zobrazovac>pushbutton4_Callback (line 156)
x_pointsQRS = MyCustomPushButtonFunctionQRS10(VxQRS);
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in VKG_Zobrazovac (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)VKG_Zobrazovac('pushbutton4_Callback',hObject,event data,guidata(hObject))
Error while evaluating UIControl Callback

Sign in to comment.

More Answers (0)

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!