Reference to non-existent field

1 次查看(过去 30 天)
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
  2 个评论
Oleg Komarov
Oleg Komarov 2012-5-29
Yes provide a synthetic example.
Oleg Komarov
Oleg Komarov 2012-5-30
I edited your original post adding your answer. This way your post will not look like answered (since it's not) and will still attract the attention of the contributors.

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2012-5-30
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

更多回答(4 个)

Oleg Komarov
Oleg Komarov 2012-5-30
I changed several things:
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');
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]);
h.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);
function MWC5plusOKpushbutton_Callback(varargin)
scalar=get(h.MWC5plusedit,'string')
end
end
  1. I moved MWC5plusOKpushbutton_Callback() to nested function (it can see h.MWC5plusedit w/o passing it explicitely)
  2. I return the handle of the pushbutton directly to h.MWC5plusedit
  3. I query the 'string' property not the 'value'
  1 个评论
Oleg Komarov
Oleg Komarov 2012-5-30
Emre's comment moved here:
Hi Oleg,
Thank you very much for your answer. Is there anyway that I can do this without nested function? That is, I would prefer MWC5plusOKpushbutton_Callback outside of the main function.

请先登录,再进行评论。


Walter Roberson
Walter Roberson 2012-5-30
Nothing is going to be in guidata() unless you put it there, which you never do. GUIDE automatically adds into guidata all the handles of all of the tagged elements that it is managing, and you have not done that.
Secondly, "handles" is not automatically passed to callbacks. The callbacks that GUIDE creates are strings of code that get executed to retrieve handles and pass it in to the appropriate routine, as it is not one of the two items automatically passed. You do not need to pass it in, by the way, not in your situation: inside your callback, you can guidata() on the value of the first argument to the routine. Well, you could if you hadn't thrown away the first argument with "~" ...
  1 个评论
Seyhan Emre Gorucu
Seyhan Emre Gorucu 2012-5-30
Hi Walter,
I understand I could not connect inputs and outputs somehow. I would appreciate if you could just copy paste the corrected lines. I could then do analogy.
Thank you,

请先登录,再进行评论。


Seyhan Emre Gorucu
Seyhan Emre Gorucu 2012-5-30
The reason I am asking is my real code is much more complicated and I would like to be flexible.
Thank you,
Emre
  1 个评论
Oleg Komarov
Oleg Komarov 2012-5-30
If you move all the functions to nested you will be able to call each other. However you can check Walter's solution.

请先登录,再进行评论。


Seyhan Emre Gorucu
Seyhan Emre Gorucu 2012-5-30
Thank you.

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by