How could I get the handler of an uiobject inside a function?

3 次查看(过去 30 天)
For example, I have this script main:
In main I call a function( func1) that creates a figure and some uiobjects;
func1 should looks like this:
function [return1,return2]= func1(arg1,arg2,arg3,etc)
figure('Name','notRelevant',...
'Units','normalized',...
'Position',[0.1 0.2 0.7 0.6],...
'NumberTitle','off','color','#B2BABB');
%uipanel
--
%
uicontrol('Style','PopupMenu',...
'Units','normalized',...
'Position',[0.26 0.48 0.1 0.15],...
'String',someString,...
'Value','1',...,
'Tag','d',...
'Callback','func2(neededHandler)',...
'Parent',Rgr);%I have the buttons in an ui panel
neededHandler=uicontrol('Style','PopupMenu',...
'Units','normalized',...
'Position',[0.26 0.68 0.1 0.15],...
'String',string1,...
'Value',1,...,
'Tag','d',...
'Callback','',...
'Parent',Rgr);
end
Inside func2:
function func2(neededHandler)
x=get(gcbo,'Value');
switch x
case 1
set(neededHandler,'String',string1);%I want to change the contents of the secod popup using the 1st one;
case 2
set(set(neededHandler,'String',string2);
...
end
end
I get an error saying that neededHandler cannot be found,ineed it does no appear in the workspace.I know that functions use some sort of 'private' workspace but I don't know how to acces them.

回答(1 个)

Divyajyoti Nayak
Divyajyoti Nayak 2024-10-17
I see that you’re trying to pass a ‘uicontrol’ object handle as an argument to a callback function but you’re getting an error saying the handle cannot be found. This is because the callback function is being created before the ‘neededHandler’ object is being defined, hence MATLAB is not able to find the handle. Defining the callback function after the ‘neededHandler’ object should resolve the error. I edited your code to show the solution:
clear;
clc;
func1();
function func1()
figure('Name','notRelevant',...
'Units','normalized',...
'Position',[0.1 0.2 0.7 0.6],...
'NumberTitle','off','color','#B2BABB');
Rgr = uipanel;
popup = uicontrol('Style','popupMenu',...
'Units','normalized',...
'Position',[0.26 0.48 0.1 0.15],...
'String',"Some String",...
'Value',1,...,
'Tag','d',...
'Parent',Rgr);%I have the buttons in an ui panel
neededHandler=uicontrol('Style','PopupMenu',...
'Units','normalized',...
'Position',[0.26 0.68 0.1 0.15],...
'String','string1',...
'Value',1,...,
'Tag','d',...
'Callback','',...
'Parent',Rgr);
popup.Callback = {@func2,neededHandler};
end
Another issue I see in the code is in the arguments of the callback function. When a callback function is called, MATLAB automatically passes two arguments (‘source’ and ‘event’) to it. Therefore to use a function as a callback function, the first two arguments should be reserved for these. Here’s the documentation for it:
Here’s how the callback function should look like:
function func2(~,~,neededHandler)
x=2;
switch x
case 1
set(neededHandler,'String',"string1");%I want to change the contents of the secod popup using the 1st one;
case 2
set(set(neededHandler,'String','string2'));
end
end
Hope this helps!

类别

Help CenterFile Exchange 中查找有关 Graphics Object Programming 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by