Azzera filtri
Azzera filtri

Reference to non-existent field

10 visualizzazioni (ultimi 30 giorni)
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 Commenti
Oleg Komarov
Oleg Komarov il 29 Mag 2012
Yes provide a synthetic example.
Oleg Komarov
Oleg Komarov il 30 Mag 2012
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.

Accedi per commentare.

Risposta accettata

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

Più risposte (4)

Oleg Komarov
Oleg Komarov il 30 Mag 2012
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 Commento
Oleg Komarov
Oleg Komarov il 30 Mag 2012
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.

Accedi per commentare.


Walter Roberson
Walter Roberson il 30 Mag 2012
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 Commento
Seyhan Emre Gorucu
Seyhan Emre Gorucu il 30 Mag 2012
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,

Accedi per commentare.


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

Accedi per commentare.


Seyhan Emre Gorucu
Seyhan Emre Gorucu il 30 Mag 2012
Thank you.

Categorie

Scopri di più su Interactive Control and Callbacks in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by