2016-02-29 20 views
0

我已經創建了一個GUI函數,如下所示。我想要做的是將這個GUI放在一個循環中,以便我可以將它用於不同的元素。對於輸出我有兩個向量是6乘1.我想要做的是,當我選擇不同的單選按鈕,並在GUI的編輯文本中放置不同的值,將結果保存在輸出的不同位置矢量取決於單選按鈕。我試圖給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) 
+0

如果GUI調用一個函數並將返回值分配給由單選按鈕定義的元素,那不是更好嗎? – Crowley

+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函數,該函數根據RB句柄將新內容分配給ValuesValues僅用於保存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值。

在這個例子中,它將隨機值賦值給定義的點,但是您可以從工作區/父函數傳遞任何變量並調用任何函數。

+0

謝謝我考慮了一些變化,它的工作原理 –

相關問題