将数据从按钮对象传输到外部函数[考虑到函数是 运行]
Transfer the data from pushbutton object to an external function[Considering the function is running]
我有这样的 matlab GUI,它有一个开始按钮、更新按钮和一个编辑文本框。
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
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
考虑从这样的脚本调用上述函数
%scriptname: external_script
myprog
现在的问题是:
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.
第4步,我该怎么做?从 GUI 按下按钮时,如何简单地将数据带到函数内的 while 循环[不是在函数之上,而是在 while 循环内循环]?
有人有想法吗?请分享。
谢谢。
编辑:
****注意**** 上面的 MY myprog 是一个虚拟程序,不考虑我只想 "PASS THE DATA FROM GUI TEXT EDITOR INTO A WHILE LOOP INSIDE A EXTERNAL FUNCTION".
您需要更改 myprog
的内容以检查是否提供了输入(使用 exist
),如果没有则提示用户输入值。
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
那么你的回调就是
function pb2_Callback(hObject, eventdata, handles)
mydata = str2double(get(handles.edit1,'string'));
myprog(mydata);
end
另一种方法是将myprog
分解为两个函数。一个负责收集用户的输入,另一个负责实际处理数据。然后,您可以从 GUI 调用处理函数。
注意:Please don't use i
or j
as variables。
主要思想是使用基础工作区作为按钮回调和 运行ning 脚本之间的变量传递机制;用 assignin
写变量
function pb2_Callback(hObject, eventdata, handles)
assignin('base', 'mydata', str2double(get(handles.edit1,'string'));
end
并使用 evalin
:
获取变量
function k = myprog(i)
while(i < 100000)
clc;
i = evalin('base', 'mydata') + 1;
k = i + 2
tic;
toc;
end
end
变量mydata
必须在调用myprog
函数之前初始化。此外,更新按钮回调需要更多努力,以避免 mydata
变量中出现不需要的值。
请注意,要使其正常工作,您需要从外部脚本启动 GUI,而不是相反。这是因为您的按钮的 'BusyAction'
属性 只能通过两种方式配置:1) 等待 运行ning 回调完成执行,或 2) 取消其如果其他回调是 运行ning,则自己执行。在这两种情况下,如果您的脚本是 运行 作为回调,则您无法在另一个回调 运行 期间影响它。
我有这样的 matlab GUI,它有一个开始按钮、更新按钮和一个编辑文本框。
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
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
考虑从这样的脚本调用上述函数
%scriptname: external_script
myprog
现在的问题是:
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.
第4步,我该怎么做?从 GUI 按下按钮时,如何简单地将数据带到函数内的 while 循环[不是在函数之上,而是在 while 循环内循环]?
有人有想法吗?请分享。 谢谢。
编辑: ****注意**** 上面的 MY myprog 是一个虚拟程序,不考虑我只想 "PASS THE DATA FROM GUI TEXT EDITOR INTO A WHILE LOOP INSIDE A EXTERNAL FUNCTION".
您需要更改 myprog
的内容以检查是否提供了输入(使用 exist
),如果没有则提示用户输入值。
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
那么你的回调就是
function pb2_Callback(hObject, eventdata, handles)
mydata = str2double(get(handles.edit1,'string'));
myprog(mydata);
end
另一种方法是将myprog
分解为两个函数。一个负责收集用户的输入,另一个负责实际处理数据。然后,您可以从 GUI 调用处理函数。
注意:Please don't use i
or j
as variables。
主要思想是使用基础工作区作为按钮回调和 运行ning 脚本之间的变量传递机制;用 assignin
function pb2_Callback(hObject, eventdata, handles)
assignin('base', 'mydata', str2double(get(handles.edit1,'string'));
end
并使用 evalin
:
function k = myprog(i)
while(i < 100000)
clc;
i = evalin('base', 'mydata') + 1;
k = i + 2
tic;
toc;
end
end
变量mydata
必须在调用myprog
函数之前初始化。此外,更新按钮回调需要更多努力,以避免 mydata
变量中出现不需要的值。
请注意,要使其正常工作,您需要从外部脚本启动 GUI,而不是相反。这是因为您的按钮的 'BusyAction'
属性 只能通过两种方式配置:1) 等待 运行ning 回调完成执行,或 2) 取消其如果其他回调是 运行ning,则自己执行。在这两种情况下,如果您的脚本是 运行 作为回调,则您无法在另一个回调 运行 期间影响它。