MATLAB Answers

0

Display figure returned by .m function into GUI axis

Elena Martín さんによって質問されました 2019 年 2 月 21 日
最新アクティビティ Rik
さんによって 回答されました 2019 年 2 月 22 日
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')

  2 件のコメント

Rik
2019 年 2 月 21 日
This is one of my problems with GUIDE: that entire wall of code has only a few relevant lines (even if you argue that your own commented lines are relevant as well). A word of warning about your OpeningFcn: it is bad practice to poof variables into your workspace. You should load into a structure, which then clearly shows where each variable is coming from and which might get overwritten.
As for your question: if a function returns no output, but generates a figure, the only thing you can do is getting the contents of that figure and copyobj them, or recreate the plot by harvesting the data that is in properties like CData, XData, YData, etc.
The much more relevant thing for us to know is that function you are calling. The rest of your code is mostly bloat from GUIDE.
My small guide to avoid GUIDE:
  • Make a figure (with f=figure;) and look into the doc for figure which properties you want to turn off (you probably want to set Menu and Toolbar to 'none')
  • Create buttons and axes and everything you need with functions like uicontrol and axes. Save the handles to each element to fields of a struct (like handles.mybutton=uicontrol(___);)
  • When you've finished loading all data (and saving it to fields of your handles struct), and creating all the buttons, save your handles struct to the guidata of your figure like this guidata(handles.f,handles);. (You can also use getappdata and setappdata)
  • You can set the Callback property of many objects. If you do, use a function name with an @ in front, or a char array that can be evaluated to valid code. (like @MyFunction or 'disp(''you pushed the button'')')
  • Callback functions will be called with two arguments: the first is a handle to the callback object, the second is eventdata that may contain special information. To get access to your data, just use handles=guidata(gcbo);. You can replace the gcbo function with the name of the first input to your callback function if you prefer.
  • More information about callbacks can be found in multiple places in the doc, for example here.
Walter Roberson
2019 年 2 月 21 日
Adding a small bit of emphasis: axes or panels or figures cannot contain figures. You might be able to move parts of the figure to your destination but not the whole thing. It is often easier to instead change the plotting routine to be able to plot into aa destination .

サインイン to comment.

2 件の回答

Rik
回答者: Rik
2019 年 2 月 22 日
 採用された回答

(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);

  0 件のコメント

サインイン to comment.


Jan
回答者: Jan
2019 年 2 月 21 日

"setting a figure returned by a .m file into matlab guide axis" - this is not clear yet. I guess the "m file" is the function Electrodes_Brain, but we do not know any detail about it. So we cannot know, what you want to move. It would be smarter in general, if this function accepts the axes to draw in as input.
set(gca,'Tag','axes1')
This creates a new axes, if there is no current one. Better: Create the axes in GUIDE and set
handles.axes1
as parent of the drawn objects.
By the way, you can replace
c = cell(1,24);
for i=1:24
c(1,i) = num2cell(1);
end
by
c = cell(1,24);
c(:) = {1};
or
c = rempat({1}, 1, 24);

  7 件のコメント

Elena Martín 2019 年 2 月 22 日
Thank you very much, I've followed all the steps you've said but it doesn't work, I still get two different figures. Thanks for the advices on the load stuff, I've changed it!
Rik
2019 年 2 月 22 日
You either use the function figure somewhere, or gca cant find an axes and create a new one.
Can you attach both your new Electrodes_Brain, as well as your edit of ft_plot_mesh? We might be able to follow where a new figure is created. You can also use the debugger yourself to go through your code step by step.
Elena Martín 2019 年 2 月 22 日
The problem is solved, I was creating a new figure... Thank you very much, I couldn't have done it without your help!
Captura.JPG

サインイン to comment.



Translated by