sending axes as parameter in matlab - matlab

I need a help with MatLab GUI .
I have a GUI with an axes on it,
and a function plotData(axes,data) which has axes as parameter.
The GUI has a button "plot Data".
How can I do the following:
When the button is clicked, call the function plotData with the parameter axes1 and the data that I want to plot?
I want the plot to be directed to axes1 which exists in the GUI.
It's suppose to be simple, but when I send the axes as parameter it doesn't plot on the GUI, or maybe it does but I cant see it.
It works fine for me without the function: just to plot the data. but to plot the data it's not 1 row :).
i tried calling ax which storing the GUI's axes handle on different M file, but since i call it as a function from different M file nothing happens with the GUI axes handle but it also not returning any error.

Side remark: Your question is a bit unclear: if you added small code snippet to illustrate what you have tried, better answers could be provided.
To the question at hand:
Have you tried directing the plot to axis1 in plotData?
function [] = plotData( ax, data )
% make ax the current axes for plot
axes( ax );
% continue with plotting the data
% ...
You can achieve the effect of axes( ax ); in a more efficient way through the specific plot commands you are using.
For example, if you are using simple plot
plot( ax, data ); % plots data to axes ax
check the documentation of the specific plot command you are using for the axes argument.

Related

Plot within a plot in MATLAB

I am trying to create a smaller plot within a plot in MATLAB, for example like the image of this MATLAB File Exchange Upload.
There, two figures are created and then both of them are plotted in one figure.
My problem however is that I already have two MATLAB figures from earlier simulations and I need to embed one figure into the other one, i.e., one would be small and the other plot would be big but in the same graph. Could someone suggest an easy way to do this?
This can be done using the copyobj function. You'll need to copy the Axes object from one figure to the other:
f(1) = openfig('fig1.fig');
f(2) = openfig('fig2.fig');
ax(1) = get(f(1),'CurrentAxes'); % Save first axes handle
ax(2) = copyobj(get(f(2),'CurrentAxes'),f(1)); % Copy axes and save handle
Then you can move and resize both axes as you like, e.g.
set(ax(2),'Position', [0.6, 0.6, 0.2, 0.2]);

unable to plot on axes in matlab gui from listbox

I am working on MATLAB GUI in which i am updating the work-space variables in a list box and then trying to plot them on axes in GUI.
I have one other push button for performing plotting operation. But when i click on plot button, i get plots in a figure which pops up.
But according to my application i have to create the plots in axes. I am unable to do so
Kindly help
MY plot button code is as follows:
function plot_button_Callback(hObject, eventdata, handles, varargin)
% hObject handle to plot_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[x] = get_var_names(handles);
if isempty(x)
return
end
if isequal(x,'a')
% figure(gcf)
try
figure(1)
evalin('base',['plot(a,b,''--r'')'])
hold all
evalin('base',['plot(a,c,''k'')'])
hold all
evalin('base',['plot(a,d,''g'')'])
figure(2)
evalin('base',['plot(a,e,''g'')'])
hold all
grid on
catch ex
errordlg(...
ex.getReport('basic'),'Error generating linear plot','modal')
end
Within each GUI callback, you have a variable called handles which is the key to editing/accessing any item in your GUI. In the case of plotting to an existing axis, you need to add an additional argument to the plot function. Here's a line of code I yanked from a GUI that I wrote:
plot(handles.axes1, xdata, ydata);
Now, this approach might not work easily for you because you are using the evalin function (which I don't recommend doing, it'd be much better to pass that information in to the gui). Regardless, a good way to implement your goal with these constraints is
a = evalin('base','a');
b = evalin('base','b');
plot(handles.axes1,a,b,'--r');
Your GUI axes might not be named axes, you'll have to check on that. You should also probably remove the figure(1) call, if I understand your goal correctly.
Also, you don't need to invoke hold all after each time you plot, once is sufficient.

How to copy a plot using copyobj

I have script that calls a function in Matlab.
The function creates a figure.
I wanted 2 figures, one that is produced by the function.
The second figure that is the exact copy of that figure produced by the function.
I do not want to rerun the function to obtain the figure.
Here is my code so far:
new_handle=copyobj(2,figure(2));
I tried to use the child and parent commands too but kept getting errors and the script would not run.
You can do it with copyobj. You only need a handle to the old axis and a handle to the new figure:
plot(1:8,randn(1,8)) %// example plot
a = gca; %// get handle to axis
f = figure; %// new figure
copyobj(a,f) %// copy axis and its children to new figure

Want to make a line, using a handle to a figure in MATLAB

I have a problem, where I already have a handle to a figure created, and I want to somehow now draw a line, given that handle. For example, I have:
f1 = figure(1);
a1 = gca;
For commands like plot and surf, I can pass the axes and/or figure handles to tell it to plot to that particular figure. However, how do you do this with the line command? There does not seem to be a way as far as I know... thank you.
The line function, like patch is a low level function. The plot command are built on top of these. However you can do this:
f1 = figure(1);
a1 = gca;
line([0 1],[0 1],'Parent',a1); % Parent has to be an axis handle
You can find more line options here: line properties or type doc Line_Props in the Matlab command window.
Have you tried the `Parent' property?
line( x, y, 'Parent', a1 );
see line properties for more info.

MATLAB - How to zoom subplots together?

I have multiple subplots in one figure. The X axis of each plot is the same variable (time). The Y axis on each plot is different (both in what it represents and the magnitude of the data).
I would like a way to zoom in on the time scale on all plots simultaneously. Ideally by using the rectangle zoom tool on one of the plots, and having the other plots change their X limits accordingly. The Y limits should remained unchanged for all of this. Auto fitting the data to fill the plot in the Y direction is acceptable.
(This question is almost identical to Stack Overflow question one Matplotlib/Pyplot: How to zoom subplots together? (except for MATLAB))
Use the built-in linkaxes function as follows:
linkaxes([hAxes1,hAxes2,hAxes3], 'x');
For more advanced linking (not just the x or y axes), use the built-in linkprop function
Use linkaxes as Yair and Amro already suggested. Following is a quick example for your case
ha(1) = subplot(2,1,1); % get the axes handle when you create the subplot
plot([1:10]); % Plot random stuff here as an example
ha(2) = subplot(2,1,2); % get the axes handle when you create the subplot
plot([1:10]+10); % Plot random stuff here as an example
linkaxes(ha, 'x'); % Link all axes in x
You should be able to zoom in all the subplots simultaneously
If there are many subplots, and collecting their axes handle one by one does not seem a clever way to do the job, you can find all the axes handle in the given figure handle by the following commands
figure_handle = figure;
subplot(2,1,1);
plot([1:10]);
subplot(2,1,2);
plot([1:10]+10);
% find all axes handle of type 'axes' and empty tag
all_ha = findobj( figure_handle, 'type', 'axes', 'tag', '' );
linkaxes( all_ha, 'x' );
The first line finds all the objects under figure_handle of type "axes" and empty tag (''). The condition of the empty tag is to exclude the axe handles of legends, whose tag will be legend.
There might be other axes objects in your figure if it's more than just a simple plot. In such case, you need to add more conditions to identify the axes handles of the plots you are interested in.
To link a pair of figures with linkaxes use:
figure;imagesc(data1);
f1h=findobj(gcf,,’type’,’axes’)
figure;imagesc(data2);
f2h=findobj(gcf,,’type’,’axes’)
linkaxes([f1h,f2h],’xy’)