How do I reference a handle structure instead of creating a local copy?

2 visualizaciones (últimos 30 días)
Hi,
I'm creating a user dialog where, depending on user selection, I'd like to populate the figure with different uicontrols. In order to make my code a bit more readable, I have created separate functions to create my uicontrols. In the example below I use a function HeightFields to create uicontrols for a specific user selection. What I would like to do is have this function create these uicontrols and update the handle structure of my figure.
However, the use of the guidata function does not update the handle structure in my main function, even if I would renew the guihandles command. So, instead I have my function generate the handle structure of the figure as an output. It works, but it's not really elegant. I have searched for an answer here and found that most likely, I'm creating a duplicate structure, rather than addressing the original one. The strange thing is that whenever I use a callback function (e.g. the TypeSelection one) rather than a regular function it does seem to work.
Would anyone be able to explain how I can reference the main handle structure instead of a copy and why the behaviour is different between a callback and other functions?
Thanks!
function y = CreateProfile
% Create figure
fh = figure('Name','Create Profile','NumberTitle','off',...
'position',[400,250,500,350]);
% Disable Menus
set(fh,'MenuBar','none');
% Create content in figure
pop_Type = uicontrol('Style','popupmenu');
pop_Type.String = {'Peak Height', 'Peak Area','Correlation'};
pop_Type.Position = [150, 280, 100, 40];
pop_Type.Callback = @TypeSelection;
% Obtain handle structure
prof_handles = guihandles(fh);
% Update handle structure
prof_handles.pop_Type = pop_Type;
guidata(fh,prof_handles);
% Create UI Controls for Peak Height
prof_handles = HeightFields(fh);
guidata(fh,prof_handles);
y = 0;
end
function TypeSelection(fh,~,~)
% Get handles structure
prof_handles = guidata(fh);
switch prof_handles.pop_Type.Value
case 1 %'Peak Height'
prof_handles = RemoveFields(fh);
guidata(fh,prof_handles)
prof_handles = HeightFields(fh);
case 2 %'Peak Area'
prof_handles = RemoveFields(fh);
guidata(fh,prof_handles)
prof_handles = AreaFields(fh);
otherwise
prof_handles = RemoveFields(fh);
end
guidata(fh,prof_handles);
end % TypeSelection
function prof_handles = RemoveFields(fh)
% Get handles structure
prof_handles = guidata(fh);
if isfield(prof_handles,'txt_Type')
delete(prof_handles.txt_Type)
prof_handles = rmfield(prof_handles,'txt_Type');
end
if isfield(prof_handles,'edit_Position')
delete(prof_handles.edit_Position)
prof_handles = rmfield(prof_handles,'edit_Position');
end
if isfield(prof_handles,'txt_Position')
delete(prof_handles.txt_Position)
prof_handles = rmfield(prof_handles,'txt_Position');
end
guidata(fh,prof_handles);
end % Remove Field
function prof_handles = HeightFields(fh)
% Get handles structure
prof_handles = guidata(fh);
txt_Type = uicontrol('Style','text');
txt_Type.Position = [50 280, 100, 40];
txt_Type.String = 'Profile Type';
prof_handles.txt_Type = txt_Type;
edit_Position = uicontrol('Style','edit');
edit_Position.Position = [150 255, 100, 20];
edit_Position.String = '3000';
prof_handles.edit_Position = edit_Position;
txt_Position = uicontrol('Style','text');
txt_Position.Position = [50 230, 100, 40];
txt_Position.String = 'Peak Position';
prof_handles.txt_Position = txt_Position;
guidata(fh,prof_handles);
end % HeightFields
function prof_handles = AreaFields(fh)
% Get handles structure
prof_handles = guidata(fh);
txt_Type = uicontrol('Style','text');
txt_Type.Position = [50 280, 100, 40];
txt_Type.String = 'Profile Type';
prof_handles.txt_Type = txt_Type;
edit_Position(1) = uicontrol('Style','edit');
edit_Position(1).Position = [150 255, 100, 20];
edit_Position(1).String = '3000';
edit_Position(2) = uicontrol('Style','edit');
edit_Position(2).Position = [300 255, 100, 20];
edit_Position(2).String = '3000';
prof_handles.edit_Position = edit_Position;
txt_Position = uicontrol('Style','text');
txt_Position.Position = [50 230, 100, 40];
txt_Position.String = 'Peak Position';
prof_handles.txt_Position = txt_Position;
guidata(fh,prof_handles);
end % AreaFields

Respuestas (0)

Categorías

Más información sobre Interactive Control and Callbacks en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by