MATLAB: Reference to non-existent field

matlab guireference to non-existent field

In MATLAB GUI, I create an editable text programmatically. Callback function of a pushbutton -created in guide- is supposed to receive the value from this editable text.
However, it gives the following error: Reference to non-existent field.
Basically, it does not recognize the editable text. I checked the tag of the editable text is correct. I also do guidata(editabletext,handles) when I create the editable text. This problem happens to me very often. Some tags are not updated at the handles hence I cannot call them from another callback function. I can provide you with the code and guide if you would like to. Thank you,
PS. I don't have this problem when I do it at the Guide. However, I want to do it programmatically.
I created this one without using guide. This time the error is the following: Not enough input arguments.
When it runs, it opens a gui. You are supposed to enter a scalar and then click OK. I want to be able to read the value inside the edit box from the call function of the pushbutton.
Thank you very much. Please find the code below:
function matlabhelp
f = figure('Visible','on',...
'MenuBar','none',...
'Units','pixels',...
'Color',[0 0 1],...
'Tag','f',...
'Position',[360,500,500,200]);
Position=get(f,'Position');
x=Position(1);
y=Position(2);
dx=Position(3);
dy=Position(4);
Intro = {'Enter a scalar'};
uicontrol('Style', 'text',...
'Units','pixels',...
'Position',[ dx/20 1.5*dy/2 5*dx/10 dy/7],...
'string', Intro,...
'BackgroundColor', [0 0 1],...
'ForegroundColor', [1 1 1]);
MWC5plusedit=uicontrol('Style', 'edit',...
'Units','pixels',...
'Position',[ 6*dx/10 1.5*dy/2 dx/5 dy/7],...
'String','',...
'Tag', 'MWC5plusedit',...
'BackgroundColor', [1 1 1],...
'ForegroundColor', [0 0 0]);
uicontrol('Style', 'pushbutton',...
'Units','pixels',...
'Position',[ 8.5*dx/10 1.5*dy/10 1.5*dx/10 dy/7],...
'String','OK',...
'Tag', 'MWC5plusOKpushbutton',...
'HorizontalAlignment', 'center',...
'Callback', @MWC5plusOKpushbutton_Callback);
handles=guidata(f);
handles=guidata(MWC5plusedit);
end
function MWC5plusOKpushbutton_Callback(~,~,handles)
scalar=get(handles.MWC5plusedit,'value')
end

Best Answer

function matlabhelp
f = figure('Visible','on',...
'MenuBar','none',...
'Units','pixels',...
'Color',[0 0 1],...
'Tag','f',...
'Position',[360,500,500,200]);
Position=get(f,'Position');
x=Position(1);
y=Position(2);
dx=Position(3);
dy=Position(4);
Intro = {'Enter a scalar'};
uicontrol('Style', 'text',...
'Units','pixels',...
'Position',[ dx/20 1.5*dy/2 5*dx/10 dy/7],...
'string', Intro,...
'BackgroundColor', [0 0 1],...
'ForegroundColor', [1 1 1]);
MWC5plusedit=uicontrol('Style', 'edit',...
'Units','pixels',...
'Position',[ 6*dx/10 1.5*dy/2 dx/5 dy/7],...
'String','',...
'Tag', 'MWC5plusedit',...
'BackgroundColor', [1 1 1],...
'ForegroundColor', [0 0 0]);
uicontrol('Style', 'pushbutton',...
'Units','pixels',...
'Position',[ 8.5*dx/10 1.5*dy/10 1.5*dx/10 dy/7],...
'String','OK',...
'Tag', 'MWC5plusOKpushbutton',...
'HorizontalAlignment', 'center',...
'Callback', {@MWC5plusOKpushbutton_Callback, MWC5plusedit} ); %CHANGE

%handles=guidata(f); %REMOVE
%handles=guidata(MWC5plusedit); %REMOVE
end
function MWC5plusOKpushbutton_Callback(~,~,edithandle) %CHANGE
scalar=get(edithandle,'string'); %CHANGE, CHANGE
end