0

我创建了一个 GUI 函数,如下所示。我想要做的是把这个 GUI 放在一个循环中,这样我就可以将它用于不同的元素。对于输出,我有两个 6 x 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)
4

1 回答 1

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 的变量和内容。
Push Button 运行根据句柄DoIt分配新内容的功能。只需要将其他值保留在 中,仅用于可视化更改。ValuesRBValuesValuesL

操作代码:

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并将其返回并根据句柄更改 y 值L

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

于 2016-02-29T17:35:38.757 回答