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

时间:2014-05-13 15:03:34

标签: matlab user-interface wait

我正在努力将信息传递到单击按钮时计算的列表框 当我使用这段代码时:

 --- 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传递我想要显示的数据,而不是使用句柄。然而,使用该方法,数据显示在列表框中,甚至在单击按钮之前它就在那里,因此它不是按钮功能中计算的数据。有没有办法让列表框等待计算信息?或者我会更好地使用除列表框之外的其他选项?

1 个答案:

答案 0 :(得分:0)

现在我实际上在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's GUI documentation)。在GUIDE GUI中,大部分初始化在幕后进行。两个button_fcn子功能类似于按键按下回调功能。

我已添加了setappdata调用,以便为该示例生成一些任意数据,以及“新数据”。按钮来模拟另一个图像中的加载。单击计算按钮可清除列表框,执行计算,更新列表框,并保存强度数据。请注意我如何使用set()修改列表框uicontrol对象的属性。 handles结构只是一组唯一ID,MATLAB用它来指向GUI的各个组件。通过使用getset,您可以获取并修改对象的各种properties。对于列表框感兴趣的是string属性,它是显示的字符串的单元格数组。

希望这个示例可以帮助您修改GUI以使其满足您的期望。