Programatically (i.e. not using guide) accessing an edit box in another function

2 views (last 30 days)
hi. I have create a figure and want to manually create a button and edit box on it too
%Add push button and edit boxes to analyse specific point
f1=figure;
ax1=subplot(1,1,1)
pb1 = uicontrol('Style', 'pushbutton',...
'String', {'Go'},...
'Position', [10 70 80 20],...
'Callback', @(src,evt) IntegratedIntensity( src, evt, ax1,handles ));
edx = uicontrol('Style', 'edit',...
'String', {'x pixel'},...
'Position', [10 40 40 20]);
I have connected the pushbutton to a function:
function IntegratedIntensity(source,event,ax,handles)
x=str2num(get(handles.edx,'String'));
However, when I call this function, the error indicates the edit box isn't recognised.
Reference to non-existent field 'edx'.
  2 Comments
Jason
Jason on 4 Apr 2017
OK, I have tried all suggestions but not getting this to work, even with the handles.figure1 suggestion. To simplify, I programmatically create a checkbox and edit box via:
checkboxCumsum= uicontrol('Style', 'checkbox',...
'String', {'Cum Sum'},...
'Position', [620 0 80 30],...
'Callback', @(src,evt) CumSum( src, evt, ax1,handles.figure1 ));
edx = uicontrol('Style', 'edit',...
'String', {'95'},...
'Position', [700 5 40 20]);
I then create a function Cumsum as:
function CumSum(source,event,ax,handles)
val = source.Value
h=getappdata(0,'histog') %previously drawn histogram
if val==1
hline = findall(gca, 'type', 'line');
delete(hline);
counts=h.Values;
edges=h.BinEdges;
width=h.BinWidth;
ctrs=edges(1:end-1)+width/2;
%integrate histogram using cumsum
cs=cumsum(counts)
l=cs(end)
l1=0.95*l
[ii]=find(cs>l1)
ct=ctrs(ii(1))
hold on
lineH1=plot([ct ct],ylim,'b--','LineWidth',0.7);
text(ct+5,0.90*max(ylim), ['CumSum95=',num2str(ct,'%.0f')],'Fontsize',8,'Color','k','FontWeight','normal')
hold off
x=str2num(get(handles.edx,'String'))
end
I still get the error
No appropriate method, property, or field 'edx' for class 'matlab.ui.Figure'.
Adam
Adam on 4 Apr 2017
handles.figure1 is a handle to the figure (assuming that is what you have named the field if you are doing it programmatically - it seems to be judging by your error).
If it where in GUIDE you would get the handles structure from this as
handles = guidata( hGUI );
where hGUI would be the 4th argument to your CumSum function, not 'handles' because it is the handle to your GUI figure.
In a programmatic UI though it depends entirely how you have programmed it as to how you need to access things. If you use a nested function then you don't need to pass anything in often. If you do it in a class then the object will usually contain everything you want so is the only needed argument.

Sign in to comment.

Accepted Answer

Adam
Adam on 27 Feb 2017
Edited: Adam on 27 Feb 2017
There are a few options, including
  1. Make IntegratedIntensity a nested function so it has access to the main function workspace
  2. Create your edit box before your pushbutton and pass its handle into the pushbutton callback along with ax1.
If you created a programmatic UI then 'handles' does not exist unless you explicitly add your components to a struct called handles so in both of the above options you should refer to the edit box simply as
edx
within the callback (assuming that is what you would call the variable you pass to the function in the 2nd option), rather than handles.edx
The fact you get a non-existent field error rather than a non-existent variable suggests 'handles' does exist in code you haven't shown us, but it certainly doesn't have the edit box attached to it.
  11 Comments
Adam
Adam on 2 Mar 2017
I always name it the same as the file. It's just convention, but I like my figure to be named something that is relevant to what it is and the filename obviously fits that purpose.
If you are only using the tag within its own UI though (as you should be generally) it doesn't really matter. If you tend to like to use findall and pull out handles from other GUIs and all that stuff (don't do it!!!) then having them all tagged figure1 is not very useful.

Sign in to comment.

More Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!