Error while evaluating UIControl Callback. Trying to get old code to work

4 views (last 30 days)
Previous colleague wrote the following Matlab code in v2015 and it worked fine. I'm encountering errors with v2023b. I've abbreviated some of the code
%Code Start
fig1=figure(1); set(fig1,'Position',window_pos+window_siz,'MenuBar','None','Name','Text1',...
'NumberTitle','off','Color',bckgr_color);
C.Line2Execute = uicontrol('Parent',fig1,'Style','edit','Units','normalized',...
'Position',[0.05,0,0.85,0.05],'BackgroundColor',bckgr_color);
C.Execute = uicontrol('Parent',fig1,'Style','pushbutton','Units','normalized',...
'HorizontalAlignment','center',...
'String','...', 'Position',[0.9,0,0.05,0.05],...
'callback','str2do=get(C.Line2Execute,''string''); try, eval(str2do); catch, disp(''Error!''); end;');
FC2_Gui %Goes to GUI Code
%FC2_Gui
C.panel(1) = uipanel('Parent',fig1,'Position',[0.05,0.05,0.9,0.9],'Title','Test2','FontWeight','Bold');
% ----- set Pressure -------
if PresManual==1,
set(C.panel(1),'Title','Input Pressure');
C.txt(1) = uicontrol('Parent',C.panel(1),'Style','text','Units','normalized',...
'HorizontalAlignment','left',...
'String','Pressure Hg_mm or PSI', 'Position',[0.05,0.85,0.25,0.1]);
C.Press = uicontrol('Parent',C.panel(1),'Style','edit','Units','normalized',...
'Position',[0.30,0.9,0.15,0.08],'BackgroundColor',bckgr_color);
uicontrol(C.Press);
while isempty(get(C.Press,'String')),
pause(1);
end
Pambient=str2num(get(C.Press,'String'))
if Pambient <500,
Pambient = Pambient*760/14.6959;
end;
set(C.txt(1),'String','Pressure Hg_mm');
set(C.Press,'String',num2str(Pambient) );
T.Pambient = Pambient;
end;
% ----- set Temperature if needed -------
if TempManual==1,
set(C.panel(1),'Title','Input Temperature');
C.txt(2) = uicontrol('Parent',C.panel(1),'Style','text','Units','normalized',...
'HorizontalAlignment','left',...
'String','Temperature, ^oC', 'Position',[0.6,0.85,0.25,0.1]);
C.Temp = uicontrol('Parent',C.panel(1),'Style','edit','Units','normalized',...
'Position',[0.85,0.9,0.11,0.08],'BackgroundColor',bckgr_color);
uicontrol(C.Temp);
while isempty(get(C.Temp,'String')),
pause(1);
end
Tambient=str2num(get(C.Temp,'String'))
end;
% ----- Select type ---------
set(C.panel(1),'Title','Select Type');
varnow = { ,1,2,3,4};
for i=length(varnow):-1:1, varnow{i+1}=varnow{i}; end; varnow{1}='';
C.txt(3) = uicontrol('Parent',C.panel(1),'Style','text','Units','normalized',...
'HorizontalAlignment','left',...
'String','Type', 'Position',[0.05,0.75,0.25,0.1]);
C.Type = uicontrol('Parent',C.panel(1),'Style','popup','Units','normalized',...
'Position',[0.30,0.78,0.15,0.1],'BackgroundColor',bckgr_color,...
'String',varnow,...
'callback','alc=get(C.panel(1),''Children''); for i=1:length(alc), if alc(i)==C.Type, myn=i; end; end; try, delete(alc(1:myn-1)); catch; end; try, delete(C.pan); catch; end; FC2_SetGas');
uicontrol(C.Type);
while get(C.Type,'Value')==1
pause(1);
end
When I make a choice on the "Type" dropdown I get the following
Unable to resolve the name 'C.panel'.
Error using FC2_Gui
Error while evaluating UIControl Callback.
This code worked before. I realize the C.Type Callback is written poorly but I'm trying to get it to work temporarily.
Thanks in advance.

Accepted Answer

Voss
Voss on 1 Nov 2023
The Callback of the C.Type popupmenu is defined as the following character vector:
'alc=get(C.panel(1),''Children''); for i=1:length(alc), if alc(i)==C.Type, myn=i; end; end; try, delete(alc(1:myn-1)); catch; end; try, delete(C.pan); catch; end; FC2_SetGas'
character vector callbacks execute in the base workspace, not in the workspace of FC2 (which FC2_Gui shares since FC2_Gui.m is a script), and evidently you don't have a variable called C in your base workspace, so that's why the global variable C is unrecognized.
There are at least a couple of ways to fix this:
  • (Recommmended) Change that character vector callback into a function. I've taken this approach in the m-files attached.
or:
  • Add the command "global C;" to the beginning of the existing character vector callback.

More Answers (0)

Categories

Find more on Structures in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!