Matlab axes gui - matlab

I generate a graph inside a function and I want to call that graph inside my gui but I don't get how can I do it.
I'm trying to do something like this
function [h1,h2] = namefuction(input)
%stuff
h1 = plot(x1,y1);
h2 = plot(x2,y2)
Then I call this function inside my .fig file and I get in the workspace a line variable for my two plots, then I try to plot them into two separate axes but I fail in this.
For now I'm doing something in order to have the plot code inside the .fig file and before i call plot(x,y) I use axes(handles.axes1); and it works, but I don't like this solution.
Does exist a solution for this?

Related

Two saved figures, want them to show in a single graph in MATLAB

Let's say I have two figures stored in separate files A.fig and B.fig which contain two separate plots. Is there a way to load A.fig and then do something like hold on and then load B.fig in the figure created for A.fig so that I have both plots in the same axes?
I think the question is not really a duplicate of this one. The OP does not ask for a way to extract the data but for a way to combine the two stored figures. Admittedly, he could extract the data and plot it again. But there is a more elegant solution...
The actual plots are children of axes which is a child of figure. Therefore you can achieve what you want by copying the children of the second axes into the first axes with copyobj. Before that, load the figures with openfig. This method has the advantage to copy different types of 'plots' (line, area, ...).
The code to copy from B.fig to A.fig is as follows and works starting from R2014b:
fig1 = openfig('A');
fig2 = openfig('B', 'invisible');
copyobj(fig2.Children.Children, fig1.Children);
If you have a Matlab version prior to R2014b, you need to use the set and get functions since you cannot use .-notation. More information can be found here. You can either use gca to get the current axes after loading the figure like this:
fig1 = openfig('A');
ax1 = gca;
fig2 = openfig('B', 'invisible');
ax2 = gca;
copyobj(get(ax2,'children'), ax1);
... or get them manually from the figure-handle like this:
fig1 = openfig('A');
fig2 = openfig('B', 'invisible');
copyobj(get(get(fig2,'children'),'children'), get(fig1,'children'));
The following script creates two figures and then applies the above code to combine them.
If you are have Matlab version R2013b or higher, replace hgsave with savefig as suggested in the documentation.
%% create two figure files
x = linspace(0,2*pi,100);
figure; hold on;
plot(x,sin(x),'b');
area(x,0.5*sin(x));
set(gca,'xlim',[0,2*pi]);
hgsave('A');
figure; hold on;
plot(x,cos(x),'r');
area(x,0.5*cos(x),'FaceColor','r');
hgsave('B');
%% clear and close all
clear;
close all;
%% copy process
fig1 = openfig('A');
fig2 = openfig('B', 'invisible');
copyobj(get(get(fig2,'children'),'children'), get(fig1,'children'));
close(fig2);
This gives the following result if manually combined in subplots:

Matlab openfig to existing figure

When in Matlab I use openfig(filename); to open a saved figure, it always opens a new window. All the 'reuse' argument does is not load the file when it appears to already be open. However, I wish to open the file into a given figure, and just overwrite its contents. Is there a way to pass a figure handle to openfig, or is there another function that would accomplish this?
So in code, what I would like to do is something along the following lines:
f = figure;
openfig(filename, 'Figure',f);
and then the figure would be displayed in figure f rather than having opened a second figure window.
I think you can arrange something close to what you want with the copyobj function. Here is a try with a docked figure:
% --- Create sample figure
h = figure;
ezplot('sin(x)');
set(gcf, 'Windowstyle', 'docked');
pause
% --- Replace the axes
clf
g = openfig('test.fig', 'invisible');
copyobj(get(g, 'CurrentAxes'), h);
delete(g);
Which gives me a smooth replacement of the axes, without flickering of the figure.
However, I don't know how this would behave with a fullscreen figure, it surely depends on the method you choose. Check also the doc of copyobj in detail, it's not copying everything so you may want to use the legacy option.

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

Plotting an existing MATLAB plot into another figure

I used the plot command to plot a figure and then changed lots of its properties using set command. I also store the handle of the plot (say h1).
What I need is to use the handle to plot the same figure again later in my code. I checked the plot command and did not find any version that accepts handle. I also thought of getting the Xdata and Ydata and use them to re-plot the same figure.
What is the simplest solution?
Edit 1: A working sample code based on copyobj that PeterM suggested.
hf(1) = figure(1);
plot(peaks);
hf(2) = figure(2);
plot(membrane);
hf(3) = figure(3);
ha(1) = subplot(1,2,1);
ha(2) = subplot(1,2,2);
for i = 1:2
hc = get(hf(i),'children');
hgc = get(hc, 'children');
copyobj(hgc,ha(i));
end
Edit 2: I also found this function that can copy figures (including legend) into a subplot.
I have run into this situation before. Depending on what you are trying to do the function copyobj may be appropriate. This function lets you take the contents of one axes and copy it to a new figure.
Improving #PeterM nice answer, one easier way would be:
fig2H=copy(gcf) % or change gcf to your figure handle
But it depends on what you want, if you want only the axes, or the whole figureā€¦ (btw, it doesn't seem to copy the legend handle).
You can use saveas to save the figure in a file, and the open to load the exact same figure from this file.
This would be the laziest way to accomplish what you want.
% Sample plot
f1 = figure(1);
plot(0:0.1:2*pi, sin(0:0.1:2*pi));
f2 = figure(2);
% The code you need
saveas(f1, 'temp.fig')
f2 = hgload('temp.fig')
delete('temp.fig')
I have used the function figs2subplots (given in Edit2 in the original question) - it does the work and is very easy to use.

sending axes as parameter in 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.