How could I get the handler of an uiobject inside a function?
1 view (last 30 days)
Show older comments
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.
0 Comments
Answers (1)
Divyajyoti Nayak
on 17 Oct 2024
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!
0 Comments
See Also
Categories
Find more on Graphics Object Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!