简体   繁体   English

如何使一个函数等待在Matlab GUI中从另一个函数传递数据?

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

I am struggling to pass information to a listbox which is calculated when a pushbutton is clicked. 我正在努力将信息传递给单击按钮时计算出的列表框。
When I use this code: 当我使用此代码时:

 --- 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)});  

This is producing the error : 这产生了错误:
Attempt to reference field of non-structure array. 尝试去引用非结构数组字段。

Error in MichelleLaycockGUImainwindow>IntensityValues_CreateFcn (line 155) MichelleLaycockGUImainwindow> IntensityValues_CreateFcn中的错误(第155行)
uiwait(handles.Intensity); uiwait(handles.Intensity);

The calculations result I would like to display in the listbox is named 'AUC' in the above code, I have tried to adapt many methods from different site examples to my code but with no luck. 我想在列表框中显示的计算结果在上面的代码中被命名为“ AUC”,我尝试将许多方法从不同的站点示例改编为我的代码,但是没有运气。
Also I have tried different code without the uiwait and pass the data I want to display using setappdata and getappdata instead of using handles. 另外,我尝试了不使用uiwait的其他代码,并使用setappdata和getappdata而不是使用句柄传递要显示的数据。 However with that method data is displayed in the listbox and it is there even before the pushbutton has been clicked so it is not the data calculated within the pushbuttonfunction. 但是,使用该方法时,数据将显示在列表框中,并且甚至在单击按钮之前就已经存在该列表框中,因此它不是按钮功能内计算出的数据。 Is there a way I can make the listbox wait for the information to be calculated? 有没有一种方法可以使列表框等待信息计算? Or would I be better off using a different option other than listbox? 还是我最好使用列表框以外的其他选项?

Now that I'm actually in front of a computer with MATLAB... 现在,我实际上正在使用MATLAB的计算机面前...

I don't think you have a full understanding of what's going on with a GUI in MATLAB. 我认为您不完全了解MATLAB中的GUI所发生的情况。 Here's a basic programmatic GUI I'm going to use to illustrate some things: 这是一个基本的编程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

You'll notice that this looks slightly different than what you'll get using GUIDE but the functionality is exactly the same (See MATLAB's GUI documentation for the differences). 您会注意到,这看起来与使用GUIDE所获得的稍有不同,但是功能完全相同(有关差异,请参见MATLAB的GUI文档 )。 In a GUIDE GUI, the majority of the initialization goes on behind the scenes. 在GUIDE GUI中,大多数初始化在后台进行。 The two button_fcn subfunctions are analogous to your button press callback functions. 这两个button_fcn子函数类似于您的按钮按下回调函数。

I've added a setappdata call to generate some arbitrary data for the example, and a 'New Data' button to simulate loading in another image. 我添加了一个setappdata调用来为示例生成一些任意数据,并添加了一个“新数据”按钮来模拟另一张图像中的加载。 Clicking on the calculate button clears the listbox, performs the calculation, updates the listbox, and saves the intensity data. 单击计算按钮将清除列表框,执行计算,更新列表框并保存强度数据。 Note how I'm using set() to modify the properties of the listbox uicontrol object. 注意我如何使用set()修改列表框uicontrol对象的属性。 The handles structure is simply a collection of unique IDs that MATLAB uses to point to the various components of your GUI. handles结构只是一组唯一ID的集合,MATLAB使用这些ID来指向GUI的各个组件。 By using get and set you can obtain and modify the various properties of your object. 通过使用getset您可以获取和修改对象的各种属性 Of interest for a listbox is the string property, which is the cell array of strings that is displayed. 列表框感兴趣的是string属性,它是显示的字符串单元格数组。

Hopefully this example helps you to modify your GUI to have it do what you're expecting. 希望本示例可以帮助您修改GUI以使其达到您的期望。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM