MATLAB: Receiving non-existent field error in GUI even though it exists

guivariables

Error Code:
—Reference to non-existent field 'shape1tx'.
—Error in Hoover_The_If_And_Switch2>process1 (line 109) set(S.shape1tx,'string','What is the radius of the Circle')
—Error while evaluating uicontrol Callback
-The error in the code is in the last else statement: else set(S.shape1tx,'string','What is the radius of the Circle')
-I cant not figure out what is wrong with it at all. When I comment of the —–entire else section, the problem just shifts to the next place where I try and -change the string in S.shape1tx. Please Help.
if true
function [] = Hoover_The_If_And_Switch2
S.fh = figure('units','pixels',...
'position',[450 200 600 500],...
'menubar','none',...
'name','Area, Perimeter, and Density',...
'resize','off',...
'numbertitle','off');
S.tx = uicontrol('style','text',...
'unit','pix',...
'position',[30 380 240 20],...
'fontsize',10,...
'string','Please Choose a Shape and push button');
S.shape = uicontrol('style','popupmenu',...
'unit','pix',...
'position',[25 350 250 25],...
'min',0,'max',2,...
'fontsize',12,...
'string',{'Square','Rectangle','Circle'});
S.pb1 = uicontrol('style','push',...
'units','pix',...
'position',[100 315 100 25],...
'HorizontalAlign','left',...
'string','Click First',...
'fontsize',11,'fontweight','demi',...
'callback',{@process1,S});
S.elementtx = uicontrol('style','text',...
'unit','pix',...
'position',[110 170 75 20],...
'fontsize',10,...
'string','Elements:');
S.element = uicontrol('style','popupmenu',...
'unit','pix',...
'position',[25 150 250 25],...
'min',0,'max',2,...
'fontsize',12,...
'string','Scandium','Yttrium,'Selenium','Krypton','Neodymium','Antimony','Astatine','Dysprosium'});
S.masstx = uicontrol('style','text',...
'unit','pix',...
'position',[105 120 90 20],...
'fontsize',10,...
'string','Mass Units');
S.mass = uicontrol('style','popupmenu',...
'unit','pix',...
'position',[25 100 250 25],...
'min',0,'max',2,...
'fontsize',12,...
'string',{'kg','g','mg'});
S.volumetx = uicontrol('style','text',...
'unit','pix',...
'position',[105 70 90 20],...
'fontsize',10,...
'string','Volume Units:');
S.volume = uicontrol('style','popupmenu',...
'unit','pix',...
'position',[25 50 250 25],...
'min',0,'max',2,...
'fontsize',12,...
'string',{'m^3','cm^3','mm^3','L','ml'});
S.shape1tx = uicontrol('style','text',...
'unit','pix',...
'position',[55 285 195 20],...
'fontsize',10,...
'string','You have not Choosen a Shape!');
S.shape1 = uicontrol('style','edit',...
'units','pix',...
'position',[25 265 240 25],...
'string',{''},...
'fontweight','demi',...
'horizontalalign','center',...
'fontsize',11);
S.shape2tx = uicontrol('style','text',...
'unit','pix',...
'position',[55 235 195 20],...
'fontsize',10,...
'string','You have not Choosen a Shape!');
S.shape2 = uicontrol('style','edit',...
'units','pix',...
'position',[25 215 250 25],...
'string',{''},...
'fontweight','demi',...
'horizontalalign','center',...
'fontsize',11);
S.tx = uicontrol('style','text',...
'unit','pix',...
'position',[310 35 275 350],...
'fontsize',11,...
'string','You have not run the program yet!');
S.pb2 = uicontrol('style','push',...
'units','pix',...
'position',[100 10 100 25],...
'HorizontalAlign','left',...
'string','Run Program',...
'fontsize',11,'fontweight','demi',...
'callback',{@process2,S});
% code

end
if true
function [] = process1(varargin)
S = varargin{3};
r = get(S.shape,'string');
if strcmp(r,'Square')
set(S.shape1tx,'string','What is the side length of the Square')
set(S.shape2tx,'string','Do not use.')
set(S.shape2,'string','No')
elseif strcmp(r,'Rectangle')
set(S.shape1tx,'string','What is the width of the Rectangle?')
set(S.shape2tx,'string','What is the length of the Rectangle?')
else
set(S.shape1tx,'string','What is the radius of the Circle')
set(S.shape2tx,'string','Do not use.')
set(S.shape2,'string','No')
end
% code
end

Best Answer

When you create S.pb1, the S you put into the Callback is copied by value according to what is already in S at that point. The S structure there at S.bp1 will not be updated with any S fields that are created after that point.
You should set() the Callback of the fields after you have created all of the S structure.