问题
I have matlab GUI like this, Which has a start pushbutton, update pushbutton and an edit text box.
function varargout = Main_function(varargin)
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Main_function_OpeningFcn, ...
'gui_OutputFcn', @Main_function_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before encdecgui is made visible.
function encdecgui_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to encdecgui (see VARARGIN)
% Choose default command line output for encdecgui
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes encdecgui wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = encdecgui_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in pb1.
function pb1_Callback(hObject, eventdata, handles)
% hObject handle to pb1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% if (mfilename(external_prog) == 0)
external_script
% else
% set(handles.pb1,'enable','off');
% end
% --- Executes on button press in pb2.
function pb2_Callback(hObject, eventdata, handles)
% hObject handle to pb2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
mydata = str2double(get(handles.edit1,'string'));
%Update mydata to external_prog's while loop
function edit1_Callback(hObject, eventdata, handles)
% hObject handle to edit1 (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 edit1 as text
% str2double(get(hObject,'String')) returns contents of edit1 as a double
% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit1 (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
% --- Executes on slider movement.
function slider1_Callback(hObject, eventdata, handles)
% hObject handle to slider1 (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,'Value') returns position of slider
% get(hObject,'Min') and get(hObject,'Max') to determine range of slider
% --- Executes during object creation, after setting all properties.
function slider1_CreateFcn(hObject, eventdata, handles)
% hObject handle to slider1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor',[.9 .9 .9]);
end
Example for external_prog
function k = myprog(i)
%prompt = 'Please enter a value to begin the count';
%i = input(prompt);
i = mydata;
while(i < 100000)
clc
i = i + 1;
k = i + 2
tic;
toc;
end
Consider the above function is being called from a script like this
%scriptname: external_script
myprog
Now the problem is:
1. I will open the GUI
2. Press Start to start the script
3. Input number into text editor [Now the external function is running]
4. Press update to take the edit text data into the while loop of the external function.
The step 4, how can I do that? How can I simply take the data to a while loop inside a function[Not on top of the function but inside the while loop while its being looped] upon pressing of pushbutton from GUI ?
anyone have an idea ? please share. Thanks.
EDIT: ****NOTE**** THE MY myprog ABOVE IS A DUMMY PROGRAM, NOT CONSIDERING myprog I JUST WANT TO "PASS THE DATA FROM GUI TEXT EDITOR INTO A WHILE LOOP INSIDE A EXTERNAL FUNCTION".
回答1:
The main idea is to use the base workspace as variable-passing mechanism between the push button callback and the running script; write the variable with assignin
function pb2_Callback(hObject, eventdata, handles)
assignin('base', 'mydata', str2double(get(handles.edit1,'string'));
end
and get the variable with evalin
:
function k = myprog(i)
while(i < 100000)
clc;
i = evalin('base', 'mydata') + 1;
k = i + 2
tic;
toc;
end
end
The variable mydata
must be initialized before calling the myprog
function. Also, the update pushbutton callback needs more effort in order to avoid unwanted values in the mydata
variable.
Please note that, for this to work, you need to launch your GUI from your external script, and not the other way around. This is because the 'BusyAction'
property of your push-button can be configured in only two ways: 1) to wait until the running callback is finished executing, or 2) to cancel its own execution if other callback is running. In both cases, if your script is run as a callback, you cannot influence it during its run from another callback.
回答2:
You will want to alter the contents of myprog
to check to see if the input is provided (using exist) and if not then prompt the user to enter the value.
function k = myprog(m)
if ~exist('m', 'var')
prompt = 'Please enter a value to begin the count';
m = input(prompt);
end
while(m < 100000)
clc
m = m + 1;
k = m + 2
tic;
toc;
end
end
Then your callback would simply be
function pb2_Callback(hObject, eventdata, handles)
mydata = str2double(get(handles.edit1,'string'));
myprog(mydata);
end
The other way is to break myprog
into two functions. One that does the gathering of inputs from the user and another that actually does the processing on the data. You could then call the processing function from your GUI.
NOTE: Please don't use i or j as variables.
来源:https://stackoverflow.com/questions/35340795/transfer-the-data-from-pushbutton-object-to-an-external-functionconsidering-the