How to specify which axes to plot in a GUI - matlab

I am new to GUI. But I have two axes in GUIDE GUI and wish to specify the one to plot a figure, but I can't find handles.axe1 anywhere. Can anyone help me with this?

Since you built your GUI with GUIDE you can easily access the Tag property of each axes in the property inspector.
Once you know the tag of each axes, you can choose where to plot stuff using the first argument of plot:
plot(TagofAxes,x,y)
or using imshow, using the Parent property:
imshow(YourImage,'Parent',TagofAxes)
and so on.

Related

How to make an axes current which is inside a figure created by GUIDE?

I created a bank figure using GUIDE and put an axes object inside it and saved the figure. Now I want to load the figure and set its axes as current axes object. This is my code:
close all; clear all; clc;
fh = openfig('test.fig');
ah = findobj(fh, 'tag', 'axes1');
figure(fh);
axes(ah);
plot(rand(10, 1));
But plot creates a new figure and plots in it! Am I missing something?
I know that I can solve it with plot(ah, ...), but I want to make gca to return this new axes. I have a lot of plotting codes that I want to be drawn in this new axes.
By default, HandleVisibility of GUIDE figures is set such that they aren't automatically detected. For example if you load the figure and then call gcf, you'll also create a new figure.
To have the plot be placed within the axes, you can specify the axes explicitly as the parent of the plot command.
plot(rand(10, 1), 'Parent', ah)
Alternately, you could specify that the HandleVisibility of the figure is 'on'. And then plot will be able to find it. This could be done by either setting the value of HandleVisibility using the property editor in GUIDE or calling the set function:
set(fh, 'HandleVisibility', 'on')
I recommend the first option as explicitly specifying the parent axes is always better than implicit.

Is it possible to link axes of Simulink scopes?

In Matlab, it is possible to link axes of different figures using linkaxes. If you zoom in one figure, the corresponding figures will be zoomed in in the same way.
I wondered if something similar was possible with Simulink scopes. It would be handy if all scopes would rescale if you manually zoom in one scope.
(An alternative would of course be to export the data to the workspace, plot it in figures and use linkaxes.)
Edit
Extending the question: would it be possible to link axes of Matlab figures and Simulink scopes?
Simulink Scope blocks are just (fancy) MATLAB figures, so most things you can do in MATLAB you can do to a figure window.
In this case, you want to do something like
% Ensure the scopes of interest are open, then
% find the handle to all of them
hscopes = findall(0,'Tag','SIMULINK_SIMSCOPE_FIGURE');
% find the handles to all axes on the scopes
ha = findall(hscopes,'Type','Axes');
% link them
linkaxes(ha);
Obviously you need to do a little more work if you only want to link specific axes.
The procedure to link figures and scopes is similar.

Matlab opens new figure in Callback despite hold

I am creating a GUI with MATLAB's GUIDE. Say, the GUI consists of an axis called axis1
and a slider called slider1. Further say I wanted to plot something (e.g. a box) into axis1 and change the box's height with the slider.
I tried doing this by adding a listener to the slider like so:
hListener = addlistener(handles.slider1,'Value','PostSet',#(src,evnt)boxHeightChange(src,evnt, handles.figure1));
in the GUI's opening function. I further defined:
function boxHeightChange(src, event, figname)
handles = guidata(figname);
% delete "old" box
delete(handles.plottedHandle);
% bring axis in focus
axes(handles.axes1);
% plot the new box (with changed size)
hold on; boxHandle = plotTheBox(event.AffectedObject.Value); hold off
handles.plottedHandle = boxHandle;
% update saved values
guidata(figname, handles);
end
This works, but always opens a new figure to plot the resizable box into instead of drawing into handles.axes1. I do not understand why, since I call axes(handles.axes1); and hold on;
Any idea that might explain the behavior?
I will post a solution to my own question.
Apparently the Callback of a listener is not declared as a "GUI Callback" which is why the GUI can not be accessed from within boxHeightChange if the GUI option "command-line accessibility" is not set to "On".
That means: In GUIDE go to Tools -> GUI options and set "Command-line accessibility" to "On".
Most plotting functions let you pass a name value pair 'Parent', ah where ah specifies the axes to plot on. I think this is the best way to handle your problem. Your actual plotting command seems to be wrapped in the plotTheBox function, so you will have to pass the axes handle in somehow.
Your plot command will look something like this:
plot(a,'Parent',handles.axes1)
You solved the problem a different way on your own, but I think you should do it my way because it's more explicit and it's less likely to lead to unforeseen problems.

Fix position of legend in MATLAB

How do I fix the position of a legend in a MATLAB figure?
I'm currently building a GUI, intended for public use, and when figures inside the GUI are produced I do not want the user to be able to move the legend by click-and-drag. Anyone?
You have to remove the buttonDownFcn from the legend.
snippet:
line(rand(1,3),rand(1,3))
l=legend('location','ne')
set(l,'ButtonDownFcn',[])

MATLAB Graphing plots with axes

I have been trying to Create a program with the GUI in MATLAB. When I try to plot information with AXES I can not figure out how to do it. I know about the function plot, but I need to be able to re-size and move the plot around in the figure so I can make room for the input uicontrol. I am not sure what to do. Please help.
There are a couple of different ways to tackle this problem. But the way I always choose to do so, is:
hold on:
plot(...), xlim([xmin xmax), ylim(ymin ymax])
This should set your bounds on the axis.
I don't have Matlab at hand right now, but try the following:
To set size of the plot axes inside the figure window use
set(gca,'Position',[left bottom width height])
see Mathworks' site on axes properties