2017-01-04 38 views
-1

我想在MATLAB中創建自己的函數來檢查一些條件,但我不知道如何在那裏發送handles。最後,我想從這個其他函數的GUI中打印一些文本。我不能直接在此函數中使用handles.t1,因爲它不能從函數內訪問。我怎麼能通過它?使用「手柄」在GUIDE GUI中在我自己的函數中打印文本

function y = check(tab) 
    if all(handles.tab == [1,1,1]) 
      set(handles.t1, 'String', 'good'); 
     else 
      set(handles.t1, 'String', 'bad'); 
    end 
end 

編輯

註釋,第一個答案之後,我決定把整個回調,我打電話給我的功能:

function A_Callback(hObject, eventdata, handles) 
if handles.axesid ~= 12 
    handles.axesid = mod(handles.axesid, handles.axesnum) + 1; 
    ax = ['dna',int2str(handles.axesid)]; 
    axes(handles.(ax)) 
    matlabImage = imread('agora.jpg'); 
    image(matlabImage) 
    axis off 
    axis image 
    ax1 = ['dt',int2str(handles.axesid)]; 
    axes(handles.(ax1)) 
    matlabImage2 = imread('tdol.jpg'); 
    image(matlabImage2) 
    axis off 
    axis image 
    handles.T(end+1)=1; 
    if length(handles.T)>2 
     check(handles.T(1:3)) 
    end 
end 
guidata(hObject, handles); 
+0

「我自己的功能」可以*更*不明確?這是編程GUI的一部分嗎? GUIDE GUI?隨機MATLAB功能?類定義?腳本?這個函數如何被調用?請參閱:[mcve] – excaza

+0

是的,這是GUIDE GUI。當我寫作「我自己的功能」時,我認爲它與回調函數不同。這個函數被稱爲「檢查」,它檢查數組是否具有與您在「if」中看到的值相同的值。我需要的是知識我怎樣才能在我自己聲明的函數中使用「句柄。(whatever_here)」。 – soommy12

回答

2

你需要使用guidata檢索handles結構該GUIDE自動在回調之間傳遞。您還需要找到figure的句柄以檢索guidata,我們將使用findallTag屬性(下面我以mytag爲例)來定位GUI圖。

handles = guidata(findall(0, 'type', 'figure', 'tag', 'mytag')); 

如果輸入參數tab是一個句柄圖形對象中你的身影,你可以叫guidata得到handles結構

handles = guidata(tab); 

更新

在您更新您的問題時,由於您要撥打check直接從回調,只需通過必要的變量,你的函數,然後對它們進行操作,通常

function y = check(tab, htext) 
    if all(tab == [1 1 1]) 
     set(htext, 'String', 'good') 
    else 
     set(htext, 'String', 'bad') 
    end 
end 

,然後從回調

if length(handles.T) > 2 
    check(handles.T(1:3), handles.t1); 
end 

或者內,您可以通過整個handles struct您的check函數

function check(handles) 
    if all(handles.tab == [1 1 1]) 
     set(handles.t1, 'string', 'good') 
    else 
     set(handles.t1, 'String', 'bad') 
    end 
end 

然後在你的回調內

if length(handles.T) > 2 
    check(handles) 
end 
+0

我根本不知道這個主意。我已經更新了這個問題。你能給我更詳細的答案,指明我的問題嗎? – soommy12

+1

@Buszman你知道GUIDE gui的'Tag'屬性嗎?你應該可以在屬性中設置它。或者,我根據您的更新提供了一個更新,告訴您如何直接將所需的信息傳遞給您的「檢查」功能。 – Suever

+0

我不知道標籤屬性。我如何檢查它?我想我需要傳遞整個句柄,因爲我有四個文本框(t1,t2,t3和t4) – soommy12

相關問題