Import figures to MATLAB GUI using handles?

1.7k views Asked by At

How can I literally take these figures and place them in the axes windows of my GUI?

I am not sure where to place handles in my user-defined code in the example below. I have 4 figures in total which look similar to this example. I want the 4 figures to be displayed in my GUI window and not in separate windows, so i've created 4 axes windows in the .fig file.

The code for this particular figure draws a grid of 66 black and white rectangles based on whether or not a value in MyVariable is a 1 or a 0. Black if MyVariable is a 1, White if MyVariable is 0. I have a file for my .fig GUI, one file to control the GUI and one with user-defined code that links to the GUI.

function test = MyScript(handles)

lots of code in between

% Initialize and clear plot window 
figure(2); clf;

% Plot the west wall array panels depending on whether or not they are
% shaded or unshaded
for x = 1:11
     for y = 1:6
  if (MyVariable(x,y) == 1)
  rectangle('position', [x-1, y-1, 1, 1] ,'EdgeColor', 'w', 'facecolor', 'k')
  else if(MyVariable(x,y) == 0)
  rectangle('position', [x-1, y-1, 1, 1], 'facecolor', 'w')
end
end
end
end

title('West Wall Array',... 
  'FontWeight','bold')

axis off

The figure for the above code looks like this: enter image description here

The function definition contains all of my script code for all 4 plots because I didn't partition my script into individual functions earlier on.

My GUI script code contains:

   MyScript(handles);
2

There are 2 answers

1
Semjon Mössinger On BEST ANSWER

As DMR sais, it's necesary to set the 'CurrentAxes'. For example, if you want to plot into the axis with the tag name 'axis1' you should simply add:

axes(handles.axes1);

to your code. Below is a very simple example for a figure containing a 'axis1' and 'axis2' using your code (corrected) code from above. Im not really shure wether you want to plot on an axis on your gui itself or a separate figure. I hope I covered both cases.

function varargout = Test(varargin)

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @Test_OpeningFcn, ...
                   'gui_OutputFcn',  @Test_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT

% --- Executes just before Test is made visible.
function Test_OpeningFcn(hObject, eventdata, handles, varargin)


% Choose default command line output for Test
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

plot(handles.axes2,-2*pi:0.1:2*pi,sin(-2*pi:0.1:2*pi));

% Initialize and clear plot window 


MyVariable = ones(11,6);
MyVariable(1:5,1) = 0;

axes(handles.axes1);

for x = 1:11
    for y = 1:6
        if (MyVariable(x,y) == 1)
            rectangle('position', [x-1, y-1, 1, 1] ,'EdgeColor', 'w', 'facecolor', 'k');
        elseif(MyVariable(x,y) == 0)
            rectangle('position', [x-1, y-1, 1, 1], 'facecolor', 'w');
        end
    end
end

title('West Wall Array',... 
  'FontWeight','bold')

figure(2); clf;

for x = 1:11
    for y = 1:6
        if (MyVariable(x,y) == 1)
            rectangle('position', [x-1, y-1, 1, 1] ,'EdgeColor', 'w', 'facecolor', 'k');
        elseif(MyVariable(x,y) == 0)
            rectangle('position', [x-1, y-1, 1, 1], 'facecolor', 'w');
        end
    end
end

title('West Wall Array',... 
  'FontWeight','bold')

function varargout = Test_OutputFcn(hObject, eventdata, handles) 

varargout{1} = handles.output;

Your guide GUI should look like this: enter image description here

And your result like this: enter image description here

2
DMR On

You can set the axis to plot into prior each plot command by setting the 'CurrentAxes' property of the figure.

Within GUIDE, you can tag a given axis, for example: http://www.mathworks.com/help/matlab/creating_guis/gui-with-multiple-axes-guide.html . Then within your drawing code, indicate which axis should be plotted into via the 'set' function and 'CurrentAxes' property.

A simple example is below, though it doesn't use GUIDE, only basic subplot axis handles:

% plots in most recent axis by default (ax2)
fig = figure;
ax1 = subplot(1,2,1);
ax2 = subplot(1,2,2);
plot(rand(1,10));

% indicate that you want to plot in ax1 instead
fig = figure;
ax1 = subplot(1,2,1);
ax2 = subplot(1,2,2);
set(gcf, 'CurrentAxes', ax1);
plot(rand(1,10));