2014-11-03 100 views
0

我正在創建一個非常簡單的GUI。 用戶輸入的一些參數由繪製圖表的函數獲取。 該函數使用另一個函數來計算我們所說的「d」的值。這些功能保存在不同的.m文件中。獲取函數變量的值到GUI

我怎麼能得到那個「d」的功能,並把它放在圖形用戶界面的某個地方後,點擊按鈕(例如在編輯文本)?

問題是我不能直接獲取d,因爲它存儲在另一個文件中,我不知道從函數中獲取它的命令。

感謝您的幫助!我希望它不是模糊的。

+1

你不能只是返回變量作爲輸出參數並傳遞它? – chappjc 2014-11-03 23:31:43

+0

非常感謝! 選項B是我真正需要的,儘管我希望在不改變ABCFunction的代碼的情況下(添加「setappdata(0,'dInFunction',d)」)是可能的。 無論如何,它的工作,感謝一百萬! – poniek 2014-11-04 09:21:24

+0

不客氣@poniek。請考慮upvoting /接受我的答案,如果它解決了你的問題。感謝和高興它的工作! – 2014-11-04 14:35:10

回答

1

我認爲@chappjc提供的選項是獲得價值的最佳方式。我建議另一種方法,您可能會發現有用,使用setappdata和getappdata。這些用於在某些工作空間(例如,基本工作空間或特定功能的工作空間)中存儲變量,並可用於在功能之間共享數據。在這個例子中,我使用了Matlab的根,即'base'工作區。

在下面的代碼中有兩個函數,一個叫做ABCFunction,它計算d和一個叫做ABCGUI的函數,它是一個簡單的GUI來演示這個點。基本上用戶在3個編輯文本框中輸入3個值,按下按鈕後,通過功能'ABCFunction'計算d,輸出顯示在編輯框中。

chappjc提出的方法稱爲選項A,使用setappdata/getappdata的選項是選項B.您可以在兩個函數中註釋適當的代碼以便四處播放並查看它是如何工作的。結果是一樣的。所以,在這裏,他們是:

1) ABC功能

function d = ABCFunction(a,b,c) 

%// Option A 
d = a*b+c; 

%//Option B 
d = a*b+c; %// Does not change from Option A. 
setappdata(0,'dInFunction',d); %// Assign the result, d, in a variable that you will call from your main GUI. See below. 
end 

2) ABCGui功能

function ABCGui(~) 

% Create the GUI figure. 
handles.figure = figure('Visible','on','Position',[360,500,300,285]); 

handles.texta = uicontrol('Style','text','String','Enter a',... 
    'Position',[50,140,60,15]); 

handles.edita = uicontrol('Style','edit','String','',... 
    'Position',[50,120,60,20]); 


handles.textb = uicontrol('Style','text','String','Enter b',... 
    'Position',[120,140,60,15]); 

handles.editb = uicontrol('Style','edit','String','',... 
    'Position',[120,120,60,20]); 


handles.textc = uicontrol('Style','text','String','Enter c',... 
    'Position',[190,140,60,15]); 

handles.editc = uicontrol('Style','edit','String','',... 
    'Position',[180,120,60,20]); 

handles.Button = uicontrol('Style','pushbutton','String','Calculate d','Position',[50,60,80,15],'Callback',@PushbuttonCallback); 

handles.textd = uicontrol('Style','text','String','d = a*b+c',... 
    'Position',[80,90,80,15]); 

handles.textResult = uicontrol('Style','text','String','',... 
    'Position',[175,90,60,15]); 

guidata(handles.figure,handles) %// Update handles structure. 
%============================================================ 

    %// Setup callback of the pushbutton 
    function PushbuttonCallback(~,~) 

     handles = guidata(gcf); %// Retrieve handles structure 

     A = str2num(get(handles.edita,'String')); 
     B = str2num(get(handles.editb,'String')); 
     C = str2num(get(handles.editc,'String')); 

     %// Option A 
     d = ABCFunction(A,B,C); %// Call the function and assign the output directly to a variable. 

     %// Option B 
     ABCFunction(A,B,C); %// Call the function and fetch the variable using getappdata. 
     d = getappdata(0,'dInFunction'); 

     set(handles.textResult,'String',num2str(d)); 

    end 
end 

圖形用戶界面非常簡單,看起來像下面這樣:

enter image description here

正如您所看到的,將變量分配給函數輸出要簡單得多。如果這是不可能的,你可以選擇選項B.希望有幫助!

+0

他不會,但我會。 ;)希望我也能接受。 – chappjc 2014-11-04 18:14:19

+0

哈哈哈謝謝! :) – 2014-11-04 18:17:28