MATLAB Answers

Updating edit text box in GUI

35 views (last 30 days)
James Hendren
James Hendren on 10 Jul 2013
So I need to pass a number from an edit text box in the GUI to an equation, and then graph it. The edit text box is the angle of incidence or phi. So whatever number I type I need it to equal phi. The using the phi/angle of incidence I can find my my values for eps1 &eps2. Help please
function AOI_Callback(hObject, eventdata, handles)
% hObject handle to AOI (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of AOI as text
% str2double(get(hObject,'String')) returns contents of AOI as a double
% --- Executes during object creation, after setting all properties.
function AOI_CreateFcn(hObject, eventdata, handles)
% hObject handle to AOI (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
global phi delta psi
psi=A(:,2)
delta=A(:,3)
phi= aoi
a=sin(phi).^2
b=sin(delta).^2
c=tan(phi).^2
d=1+tan(phi).^2
e=tan(psi).^2
f=1-tan(psi).^2
g=[1-2*tan(psi)*cos(delta)+tan(pis).^2].^2
h=4*tan(psi)*sin(delta)
eps1= (a*d*f.^2-4*e*b)./g
eps2 = (a*c*h*f)./g

  0 Comments

Sign in to comment.

Accepted Answer

Evan
Evan on 10 Jul 2013
Instead of putting your equation in the CreateFcn, put it in AOI_Callback.
Also, using the handles structure instead of global variables is much safer and easier, especially since you're using GUIDE to create your GUI. Something like this should work:
function AOI_Callback(hObject, eventdata, handles)
% hObject handle to AOI (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of AOI as text
% str2double(get(hObject,'String')) returns contents of AOI as a double
handles.phi = str2double(get(hObject,'String')); % Get the value entered in your editbox
%now perform your calculations here. If you want to save any of the variables you calculate, add them to the handles structure in the form handles.(variablename)
guidata(hObject,handles) %save the handles structure back out to your editbox. Now, you should be able to access the data you added to it in any other callback
% --- Executes during object creation, after setting all properties.
function AOI_CreateFcn(hObject, eventdata, handles)
% hObject handle to AOI (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end

  2 Comments

James Hendren
James Hendren on 10 Jul 2013
I'm unclear on how to use the handles structure to put the entered number into my equation. Could elaborate more please?
Evan
Evan on 10 Jul 2013
Sure. The handles structure is basically a structure that allows you to share data throughout your GUI. Using it will allow you to accomplish what you're wanting to do with global variables, except it will make your code easier to read and safer.
As you can see, all the "Callbacks" in your mfile allow three arguments to be passed in: hObject, eventdata, and handles.
  1. eventdata isn't used very often and is often just a placeholder, but if you do ever use it, it contains information that related to the object at the moment the callback is initiated.
  2. hObject is the handle to the uicontrol (editbox, pushbutton, etc.) that you're currently looking at. It's the same thing as calling handles.object_name where object_name is that "tag" of your uicontrol.
  3. handles is your handles structure. Like any other structure, it contains fields. Since every callback pulls in handles, it's a way of sharing variables between callbacks. To update the handles structure in the workplace of the callback being executed, you can call handles = guidata(hObject); This accesses hObject (the current uicontrol, in this case your editbox), and gets the version of handles saved in it. Since handles is passed in at the beginning, there are only special cases where you'll have to do this. You can add fields to handles or modify existing ones. To update handles so that your changes can be accessed in other callbacks, you call guidata(hObject,handles)
So, in short, you basically create your code like you normally would, but save any variables you want to keep for later in the handles structure. Then, at the end of every callback where you add to or modify fields of handles, you call guidata(hObject,handles).
More info:

Sign in to comment.

More Answers (1)

James Hendren
James Hendren on 10 Jul 2013
I'm getting an error when my string goes into the sin for i. Cn you help please?
% Hints: get(hObject,'String') returns contents of AOI as text
% str2double(get(hObject,'String')) returns contents of AOI as a double
handles.AOI = str2double(get(hObject,'String'));
% phi=str2double(AOI);
i=sind(AOI)^.2;
b=sind(y2).^2;
c=tand(AOI).^2;
d=1+tand(AOI).^2;
e=tand(y1).^2;
f=1-tand(y1).^2;
g=(1-2*tand(y1)*cosd(y2)+tand(y1).^2).^2;
h=4*tand(y1)*sind(y2);
eps1 = (i*d*f.^2-4*e*b)./g;
eps2 = (i*c*h*f)./g;
plot(handles.axes3,x,y1);
plot(handles.axes4,x,y2);

  1 Comment

Evan
Evan on 10 Jul 2013
It looks like there are two problems here. The first is causing your error. The second will cause problems later on:
1) You don't have a variable named AOI. Instead, you have a structure named handles, and that structure has a field named AOI. If you want to work with that field, you'll have to either call it directly or write it to a new variable. So something like this:
handles.AOI = str2double(get(hObject,'String'));
% phi=str2double(handles.AOI);
i=sind(handles.AOI)^.2;
b=sind(y2).^2;
And so on. Every time you want use that value, you need to reference it from the handles structure, because that's where it exists.
2) You need to name the value you pull out of your textbox something other than handles.AOI. This is because, along with the stuff you add to it, handles contains fields for every object in your GUI. Since your textbox's tag is "AOI," you're going to overwrite the reference to the textbox with that value you just pulled out and not be able to access the textbox again. This is why I suggested naming your angle of incidence value"handles.phi" instead of "handles.AOI."
handles.phi = str2double(get(hObject,'String'));
% phi=str2double(handles.phi);
i=sind(handles.phi)^.2;
b=sind(y2).^2;
c=tand(handles.phi).^2;
d=1+tand(handles.phi).^2;
e=tand(y1).^2;
f=1-tand(y1).^2;
g=(1-2*tand(y1)*cosd(y2)+tand(y1).^2).^2;
h=4*tand(y1)*sind(y2);
eps1 = (i*d*f.^2-4*e*b)./g;
eps2 = (i*c*h*f)./g;
plot(handles.axes3,x,y1);
plot(handles.axes4,x,y2);

Sign in to comment.

Sign in to answer this question.