为GUI提供输入并捕获输出以便在另一个函数中使用它

时间:2016-02-29 08:34:07

标签: matlab user-interface user-input matlab-figure matlab-gui

我已经创建了如下GUI功能。我想要做的是将这个GUI放在循环中,以便我可以将它用于不同的元素。对于输出我有两个向量是6乘1.我想要做的是,当我选择不同的单选按钮并在GUI的编辑文本中放置不同的值时,将结果保存在输出的不同位置矢量取决于单选按钮。我试图给GUI一个标题作为输入。

提前致谢。

function varargout = distributedloads(varargin)
% DISTRIBUTEDLOADS MATLAB code for distributedloads.fig
%      DISTRIBUTEDLOADS, by itself, creates a new DISTRIBUTEDLOADS or raises the existing
%      singleton*.
%
%      H = DISTRIBUTEDLOADS returns the handle to a new DISTRIBUTEDLOADS or the handle to
%      the existing singleton*.
%
%      DISTRIBUTEDLOADS('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in DISTRIBUTEDLOADS.M with the given input arguments.
%
%      DISTRIBUTEDLOADS('Property','Value',...) creates a new DISTRIBUTEDLOADS or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before distributedloads_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to distributedloads_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help distributedloads

% Last Modified by GUIDE v2.5 28-Feb-2016 14:52:56

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @distributedloads_OpeningFcn, ...
                   'gui_OutputFcn',  @distributedloads_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 distributedloads is made visible.
function distributedloads_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 distributedloads (see VARARGIN)

imshow('disloads.png')
% Choose default command line output for distributedloads
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes distributedloads wait for user response (see UIRESUME)
uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = distributedloads_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)
varargout = str2double(handles.DATA.EL);

function ELNUM_Callback(hObject, eventdata, handles)
% hObject    handle to ELNUM (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 ELNUM as text
%        str2double(get(hObject,'String')) returns contents of ELNUM as a double
EL = get(hObject , 'String');

handles.DATA.EL = EL;
guidata(hObject,handles)



% --- Executes during object creation, after setting all properties.
function ELNUM_CreateFcn(hObject, eventdata, handles)
% hObject    handle to ELNUM (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



function LOADVAL_Callback(hObject, eventdata, handles)
% hObject    handle to LOADVAL (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 LOADVAL as text
%        str2double(get(hObject,'String')) returns contents of LOADVAL as a double

Load = get(hObject , 'String');
handles.DATA.Load = Load;
guidata(hObject , handles)
% --- Executes during object creation, after setting all properties.
function LOADVAL_CreateFcn(hObject, eventdata, handles)
% hObject    handle to LOADVAL (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 button press in CONFIRM.
function CONFIRM_Callback(hObject, eventdata, handles)
% hObject    handle to CONFIRM (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

delete(handles.figure1)

% --------------------------------------------------------------------
function buttongroup_ButtonDownFcn(hObject, eventdata, handles)
% hObject    handle to buttongroup (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
Type = get(hObject , 'selectedObject');
handles.DATA.Type = Type;
guidata(hObject , handles)

1 个答案:

答案 0 :(得分:0)

我写了一个创建GUI和一个回调函数的示例脚本。

在GUI中,有一个用于单选按钮,按钮和带有线的轴的矢量向量,用于显示结果。

脚本代码:

close all          % close all figures
figure             % open a figure for GUI
Values=zeros(3,1); % Variable of the interest

UIGroup=uibuttongroup('parent',gcf,'position',[0 0 1 1]); % Group for Radio Buttons
for ii=1:3         % create 3 Radio buttons, for example
  RB(ii)=uicontrol('style','radiobutton',...
                   'units','normalized','position',[.05, ii/10 0.15 0.1],...
                   'parent',UIGroup,'string',['Button ' num2str(ii)]);
end
% Push Button that runs DoIt function
uicontrol('style','pushbutton','string','DO',...
          'units','normalized','position',[0.45 0.05 0.1 0.1],...
          'callback','Values=DoIt(RB,L,Values);')

% Axes and Line just for example
ax=axes('units','normalized','position',[0.25 0.2 0.6 0.7],...
        'xlim',[-0.1 3.1],'ylim',[-0.1 1.1]);
L=line('xdata',1:3,'ydata',Values,'marker','.','linestyle','none')

此脚本定义了Values变量和GUI的内容 按钮运行DoIt功能,根据Values句柄将新内容分配给RBValues只需保留Values中的其他值,L仅用于显示更改。

代码:

function[OutValues]=DoIt(RadioHandle,LineHandle,InValues)
    OutValues=InValues;        % Copy Values from input to output variable
%% Find which radio button is active
    M=max(size(RadioHandle));
    for ii=1:M
        Radios(ii)=get(RadioHandle(ii),'value');
    end
    RadioChecked=find(Radios==1);     % This RadioButton is active

    OutValues(RadioChecked)=ProcessIt;     % Process the chosen position.
    set(LineHandle,'ydata',OutValues);% Visualize the change

function[OUT]=ProcessIt()
    OUT=rand;  % this function will just return random value, for example.

DoIt函数读取单选按钮RB的句柄并确定哪个按钮处于活动状态。然后,它会更改Value变量中的相应值并将其返回,并根据句柄L更改y值。

在此示例中,它将为定义的点分配随机值,但您可以从工作空间/父函数传递任何变量并调用任何函数。