MATLAB: Display figure returned by .m function into GUI axis

figureguiguideMATLABmatlab gui

I am having some trouble setting a figure returned by a .m file into matlab guide axis, I've tried different options but none of them work. I would like to get the figure when a pushbutton is pushed but the image is plotted in a separate figure. If somebody knows how to do this please let me know. Thanks!
Here is my code:
function varargout = MeshPlot(varargin)
% MESHPLOT MATLAB code for MeshPlot.fig
% MESHPLOT, by itself, creates a new MESHPLOT or raises the existing
% singleton*.
%




% H = MESHPLOT returns the handle to a new MESHPLOT or the handle to
% the existing singleton*.
%
% MESHPLOT('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in MESHPLOT.M with the given input arguments.
%
% MESHPLOT('Property','Value',...) creates a new MESHPLOT or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before MeshPlot_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to MeshPlot_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help MeshPlot
% Last Modified by GUIDE v2.5 20-Feb-2019 18:43:12
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @MeshPlot_OpeningFcn, ...
'gui_OutputFcn', @MeshPlot_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 MeshPlot is made visible.
function MeshPlot_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure

% eventdata reserved - to be defined in a future version of MATLAB










% handles structure with handles and user data (see GUIDATA)







% varargin command line arguments to MeshPlot (see VARARGIN)
load('C:\Users\Elena\Desktop\TFG\Atlas Importantes\standard_vol.mat')
load('C:\Users\Elena\Desktop\TFG\ScriptsTFG\surface_pial_both.mat')
% cfg = [];
% cfg.dataset = [];
% handles.mesh1.pos = vol.bnd(1).pnt;
% handles.mesh1.tri = vol.bnd(1).tri;
% % mesh2.pos = vol.bnd(2).pnt;
% % mesh2.tri = vol.bnd(2).tri;
% % mesh3.pos = vol.bnd(3).pnt;
% % mesh3.tri = vol.bnd(3).tri;
% ft_plot_mesh(handles.mesh1, 'facecolor', 'cortex', 'edgecolor', 'none','unit', 'mm', 'facealpha', 0.1)
% % mesh2 = ft_plot_mesh(mesh3, 'facecolor', 'brain', 'edgecolor', 'none','unit', 'mm', 'facealpha', 0.2)
% % mesh3 = ft_plot_mesh(mesh2, 'facecolor', 'brain', 'edgecolor', 'none','unit', 'mm', 'facealpha', 0.2)
% hold on
% load('elec_realigned.mat')
% %mesh4 = ft_plot_sens(elec_realigned,'label','label','elecshape','sphere');
% handles.mesh = load('C:\Users\Elena\Desktop\TFG\ScriptsTFG\surface_pial_both.mat');
% ft_plot_mesh(handles.mesh, 'SpecularStrength', 0.3, 'facecolor', mesh.sulc, 'edgelighting', 'none','edgecolor', 'none','vertexcolor', mesh.sulc,'unit', 'mm', 'facealpha', 0.1, 'surfaceonly', 'false')
% % Choose default command line output for MeshPlot
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes MeshPlot wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = MeshPlot_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in radiobutton1.
function radiobutton1_Callback(hObject, eventdata, handles)
% hObject handle to radiobutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of radiobutton1
% --- Executes on button press in radiobutton2.
function radiobutton2_Callback(hObject, eventdata, handles)
% hObject handle to radiobutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of radiobutton2
% --- Executes on slider movement.
function slider1_Callback(hObject, eventdata, handles)
% hObject handle to slider1 (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
% --- Executes during object creation, after setting all properties.


function slider1_CreateFcn(hObject, eventdata, handles)
% hObject handle to slider1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called


% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
function editUmbral_Callback(hObject, eventdata, handles)
% hObject handle to editUmbral (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of editUmbral as text
% str2double(get(hObject,'String')) returns contents of editUmbral as a double
% --- Executes during object creation, after setting all properties.
function editUmbral_CreateFcn(hObject, eventdata, handles)
% hObject handle to editUmbral (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu1 (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu1 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu1
% --- Executes during object creation, after setting all properties.
function popupmenu1_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
c = cell(1,24);
for i=1:24
c(1,i) = num2cell(1);
end
set(gca,'Tag','axes1')
axes(findall(0,'Tag','axes1')) %set the axes as the current ones
%axes(handles.axes1);
Electrodes_Brain(c, 'cor')

Best Answer

(This is an earlier comment reposted as answer. I would suggest giving Jan's answer an upvote if you think he helped to get to a working solution)
You can edit the function to use a target axes. For those who don't have that toolbox: here's a link to the github version of the relevant function.
Note that you are not using the global variable you define at the top of Electrodes_Brain. You shouldn't be using global variables anyway. You often use mat files to poof variables into existance. At a minimum you should be loading them to a struct so it is clear where each variable is coming from. Ideally you wouldn't even use load there, but pass in those variables as input. Also, hardcoded paths are very prone to breaking your code.
You also shouldn't use Figure3D = figure;. Pass handles.axes1 as a variable to Electrodes_Brain:
function Figure3D = Electrodes_Brain(selected_electrodes, name, target_ax)
Then change your call to the plotter to something like this:
ft_plot_mesh(mesh1, 'facecolor', 'cortex', 'edgecolor', 'none','unit', 'mm', 'facealpha', 0.1, 'Parent', target_ax)
Now you need to modify either the getopt function, or put this in ft_plot_mesh:
try
L=ismember(lower(varargin),{'parent'});
parent=varargin{1+find(L,1));
catch
parent=gca;
end
And now you need to edit all the lines where a version of hs is created to include this parent as the parent object. The patch, plot and plot3 functions all support this syntax. As an example: in the linked version on github, line 258 becomes:
hs = patch('Vertices', pos, 'Faces', tri, 'Parent', parent);
Related Question