Question

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)
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.
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. 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?

Was it helpful?

Solution

Now that I'm actually in front of a computer with MATLAB...

I don't think you have a full understanding of what's going on with a GUI in MATLAB. Here's a basic programmatic GUI I'm going to use to illustrate some things:

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). In a GUIDE GUI, the majority of the initialization goes on behind the scenes. The two button_fcn subfunctions are analogous to your button press callback functions.

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. 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. The handles structure is simply a collection of unique IDs that MATLAB uses to point to the various components of your GUI. By using get and set you can obtain and modify the various properties of your object. Of interest for a listbox is the string property, which is the cell array of strings that is displayed.

Hopefully this example helps you to modify your GUI to have it do what you're expecting.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top