将数据从按钮对象传输到外部功能[考虑功能正在运行] [英] Transfer the data from pushbutton object to an external function[Considering the function is running]

查看:105
本文介绍了将数据从按钮对象传输到外部功能[考虑功能正在运行]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的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循环中).

有人有主意吗?请分享. 谢谢.

****注意****上面的myprog是一个虚拟程序,不考虑myprog,我只是想将GUI文本编辑器中的数据传递到外部函数中的整个循环中".

解决方案

主要思想是将基本工作空间用作按钮回调和正在运行的脚本之间的变量传递机制.用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

在调用myprog函数之前,必须初始化变量mydata.另外,为了避免mydata变量中不需要的值,更新按钮回调需要付出更多的努力.

请注意,要使其正常工作,您需要从外部脚本启动GUI,而不是相反.这是因为只能通过两种方式配置按钮的'BusyAction'属性:1)等待正在运行的回调完成执行,或2)如果正在运行其他回调则取消自己的执行.在这两种情况下,如果您的脚本都作为回调运行,则在运行期间无法从另一个回调影响脚本.

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".

解决方案

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.

这篇关于将数据从按钮对象传输到外部功能[考虑功能正在运行]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆