Calling Additional Functions in GUI

1 次查看(过去 30 天)
Hello Everyone!
Thanks for your attention.
I've created a GUI using GUIDE with lots of edit boxes, what I do in the edit call backs is to check if the input value is valid or not.
I've 3 push buttons, I want to acquire the edit boxes value in the push buttons call back, so I thought of creating a function to do that, also there is a series of plots. So I defined 2 functions at the end of the GUI m file, "Plotter" and "ValueCollector", to plot on the axes which is on the form, and second is to read all the edit boxes values at once.
PROBLEM is: "ValueCollector" does not work in push buttons call back, but it works in the dropdown menu call back, buttons are off until user pick a choice from the dropdown menu, then they will be on, to dropdown menu runs the "ValueCollector" for the first time (I don't know if it's related or not)
% Probe Selector is the popup menu
function Probe_Selector_Callback(hObject, eventdata, handles)
handles = valueCollector(handles);
handles.CurrentProbe.X = handles.probe.info(handles.CurrentProbe.Num,2);
handles.CurrentProbe.Y = handles.probe.info(handles.CurrentProbe.Num,3);
% some processing based on values collecter by ValueCollector which is
% correct
plotter(handles); % Plots everything
set(handles.arb_button,'Enable','on');
guidata(hObject,handles);
function arb_button_Callback(hObject, eventdata, handles)
handles = valueCollector(handles); % this is not working!!
plotter(handles);
% some processing
guidata(hObject,handles);
function [handles] = valueCollector (handles)
% this collects all the property values of the edits
handles.initial.Fs = str2num(get(handles.ArbFs_edit,'String'));
handles.initial.cycles = str2num(get(handles.cycles_edit,'String'));
handles.initial.fc = str2num(get(handles.fc_edit,'String'));
handles.initial.radSize = str2num(get(handles.pattern_edit,'String'))/2;
handles.initial.phaseShift=str2num(get(handles.phaseShift_edit,'String'))*pi/180;
handles.initial.Mu = str2num(get(handles.Mu_edit,'String'));
handles.initial.G = str2num(get(handles.G_edit,'String'));
handles.CurrentProbe.Num = get(handles.Probe_Selector,'Value');
demo = get(handles.checkbox1,'Value');
if demo == 1
handles.initial.demo = 'demo';
else
handles.initial.demo = 'non';
end
"ValueCollector" works file in the popup_callback but it does not work in 2 push buttons that I have! the variable handles that returned by ValueCollector function is not updated!
I used guidata(hObject,handles) at the end of the ValueCollector but the problem is still remains!
I tried ValueCollector(hObject,eventdata,handles) nothing chande, I add a guidata(ho...) after calling ValueCollector in the push buttons, still not working! I'm running out of ideas.
Thanks for your help in advance!

采纳的回答

Joseph Cheng
Joseph Cheng 2014-7-17
Not sure what is going on since i can't reproduce it not updating. Here is a quick test i did using a few edit boxes and pushbuttons. the first one gets and stores whats inside the edit boxes. the second buttons displays whats in handles into the static text fields.
  2 个评论
Salaheddin Hosseinzadeh
编辑:Salaheddin Hosseinzadeh 2014-7-17
Dear Joseph,
Thanks for the test, I change your test code as follows, and yet it works! It does what I exactly want to do, I'll try to see if I can fix my particular problem according to this instance!
I have a guidata(hObject,handles) at the end of one of the popup should I disable that?! I'm gonna give it a try :(
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles = ValueCollector(handles);
set(handles.text3,'String',handles.X);
% guidata(hObject,handles);
function handles = ValueCollector(handles)
handles.X = get(handles.edit1,'String');
handles.Y = get(handles.edit2,'String');
% guidata(hObject,handles);
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles = ValueCollector (handles);
set(handles.text3,'String',handles.X)
% set(handles.text4,'String',handles.Y)
Joseph Cheng
Joseph Cheng 2014-7-17
I don't think the guidata() at the end of the popup would do anything strangely. Maybe put some breakpoints in the code to and see where the handles update breaks down.

请先登录,再进行评论。

更多回答(1 个)

Image Analyst
Image Analyst 2014-7-17
Get the string value of edit boxes, not the value, even if what's in there is a number:
handles.X = str2double(get(handles.edit1,'String'));
handles.Y = str2double(get(handles.edit2,'String'));
but that's still not very robust since you are not checking if the user put in some non-numerical text instead of a number.
  1 个评论
Salaheddin Hosseinzadeh
Hi Image Analyst
Thanks for your answer
I wrote the code roughly, definitely I do this :(
I'll change the code to what exactly (almost) it is

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by