我正在努力将信息传递给一个列表框,该列表框在单击按钮时计算。
当我使用这段代码时:
--- 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而不是使用句柄显示的数据。然而,使用该方法数据显示在列表框中,它甚至在按钮被单击之前就已经存在了,所以它不是在pushbuttonfunction中计算的数据。有没有办法让列表框等待信息被计算出来?或者我最好使用不同的选项,而不是列表框?
现在我实际上是在电脑前用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
结构只是MATLAB用于指向GUI的各个组件的唯一id的集合。通过使用get
和set
,您可以获得和修改对象的各种属性。列表框感兴趣的是string
属性,它是显示的字符串的单元格数组。
希望这个例子能帮助你修改你的GUI,让它做你想要的。