将数据从按钮对象传输到外部函数[考虑到函数正在运行]

时间:2016-02-11 13:35:26

标签: matlab user-interface matlab-guide

我有像这样的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步,我该怎么做?我怎样才能简单地将数据带到函数内的while循环中[不是在函数的顶部,而是在循环时在while循环内部]按下来自GUI的按钮?

谁有个主意?请分享。 感谢。

编辑: ****注意****我的myprog上面是一个虚假的程序,没有考虑myprog我只是希望&#34;将数据从GUI文本编辑器传递到一个外部功能的循环中#34;。

2 个答案:

答案 0 :(得分:0)

您需要更改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

答案 1 :(得分:0)

主要思想是使用基础工作区作为按钮回调和运行脚本之间的变量传递机制;用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)等待运行回调完成执行,或2)如果其他回调正在运行则取消自己的执行。在这两种情况下,如果您的脚本作为回调运行,则在从另一个回调运行期间无法影响它。