簡體   English   中英

如何使一個函數等待在Matlab GUI中從另一個函數傳遞數據?

[英]How to make one function wait for data being passed from another function in a matlab GUI?

我正在努力將信息傳遞給單擊按鈕時計算出的列表框。
當我使用此代碼時:

 --- Executes on button press in CalculateIntensity.
function CalculateIntensity_Callback(hObject, eventdata, handles)
% hObject    handle to CalculateIntensity (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% Trapz function
starting_value = getappdata(0,'StartValue');
ending_value = getappdata(0,'EndValue');
StartingValue = str2num(starting_value);
EndingValue = str2num(ending_value);
A = getappdata(0,'XYarray');
%line 122 and 123 finds location of data in the entire spectrum
[~,indx1]=ismember(StartingValue,A,'rows');
[~,indx2]=ismember(EndingValue,A,'rows');
arrayfortrapz = A(indx1:indx2,1:2);

X1 = arrayfortrapz(1:end,1);
Y1 = arrayfortrapz(1:end,2);
 AUC = trapz(X1,Y1); %intergration
 handles.Intensity = AUC;
 guidata(hObject,handles);


% --- Executes on selection change in IntensityValues.
function IntensityValues_Callback(hObject, eventdata, handles)
% hObject    handle to IntensityValues (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns IntensityValues contents as cell array
% contents{get(hObject,'Value')} returns selected item from IntensityValues


% --- Executes during object creation, after setting all properties.
function IntensityValues_CreateFcn(hObject, eventdata, handles)
% hObject    handle to IntensityValues (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: listbox 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
uiwait(handles.Intensity); 
IV = handles.Intensity;

set(hObject,'String',{num2str(IV)});  

這產生了錯誤:
嘗試去引用非結構數組字段。

MichelleLaycockGUImainwindow> IntensityValues_CreateFcn中的錯誤(第155行)
uiwait(handles.Intensity);

我想在列表框中顯示的計算結果在上面的代碼中被命名為“ AUC”,我嘗試將許多方法從不同的站點示例改編為我的代碼,但是沒有運氣。
另外,我嘗試了不使用uiwait的其他代碼,並使用setappdata和getappdata而不是使用句柄傳遞要顯示的數據。 但是,使用該方法時,數據將顯示在列表框中,並且甚至在單擊按鈕之前就已經存在該列表框中,因此它不是按鈕功能內計算出的數據。 有沒有一種方法可以使列表框等待信息計算? 還是我最好使用列表框以外的其他選項?

現在,我實際上正在使用MATLAB的計算機面前...

我認為您不完全了解MATLAB中的GUI所發生的情況。 這是一個基本的編程GUI,我將使用它來說明一些事情:

function testbox
handles = initializeGUI;
resetList(handles)
XYarray = rand(10,2);
setappdata(handles.mainwindow,'XYarray',XYarray);
end

function [handles] = initializeGUI
handles.mainwindow = figure('MenuBar','None');
handles.button = uicontrol( ...
    'Style','pushbutton', ...
    'Units','normalized', ...
    'Position',[0.2 0.2 0.3 0.08], ...
    'String','New Data Button', ...
    'Callback',{@newdatabutton_fcn} ...
    );
handles.button = uicontrol( ...
    'Style','pushbutton', ...
    'Units','normalized', ...
    'Position',[0.5 0.2 0.3 0.08], ...
    'String','A Calculate Button', ...
    'Callback',{@calculatebutton_fcn} ...
    );
handles.listbox = uicontrol( ...
    'Style','listbox', ...
    'Units','normalized', ...
    'Position',[0.2 0.3 0.6 0.6] ...
    );

guidata(handles.mainwindow, handles);
end

function newdatabutton_fcn(hObject,~)
% Executes on button press
% Generates new XYarray data
handles = guidata(hObject);
resetList(handles)
XYarray = rand(10,2);
setappdata(handles.mainwindow,'XYarray',XYarray);
setappdata(handles.mainwindow,'intensity',[]);
end

function calculatebutton_fcn(hObject,~)
% Executes on button press
% Performs arbitrary calculation
handles = guidata(hObject);
resetList(handles)
XYarray = getappdata(handles.mainwindow,'XYarray');
intensity = XYarray(:,1)*5;
set(handles.listbox,'String',{intensity});
setappdata(handles.mainwindow,'intensity',intensity)
end

function resetList(handles)
% Clears the listbox
set(handles.listbox,'String','')
end

您會注意到,這看起來與使用GUIDE所獲得的稍有不同,但是功能完全相同(有關差異,請參見MATLAB的GUI文檔 )。 在GUIDE GUI中,大多數初始化在后台進行。 這兩個button_fcn子函數類似於您的按鈕按下回調函數。

我添加了一個setappdata調用來為示例生成一些任意數據,並添加了一個“新數據”按鈕來模擬另一張圖像中的加載。 單擊計算按鈕將清除列表框,執行計算,更新列表框並保存強度數據。 注意我如何使用set()修改列表框uicontrol對象的屬性。 handles結構只是一組唯一ID的集合,MATLAB使用這些ID來指向GUI的各個組件。 通過使用getset您可以獲取和修改對象的各種屬性 列表框感興趣的是string屬性,它是顯示的字符串單元格數組。

希望本示例可以幫助您修改GUI以使其達到您的期望。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM