0
votes

In Matlab's GUIDE, I am trying to dynamically change a popup menu's text with the header of a data file when that file is first selected using the choose_data_button. However, it gives me the error that the group_variable_popupmenu is a non-existent field.

This post might have the answer to my question, but I don't understand what's going on: MATLAB GUI error Reference to non-existent field '---'

% --- Executes on button press in choose_data_button.
function choose_data_button_Callback(hObject, eventdata, handles)
    % hObject    handle to choose_data_button (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)

    [filename, pathname] = uigetfile( ... );
    set(handles.patient_data_file, 'string', fullfile(pathname,filename));

    data = csvimport(fullfile(pathname, filename));
    %%%%% ERROR (Reference to non-existent field 'group_variable_popupmenu'.):
    set(handles.group_variable_popupmenu, 'string', data(1,:));


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

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

    if ispc && isequal(get(hObject,'BackgroundColor'),                                 
        get(0,'defaultUicontrolBackgroundColor'))
        set(hObject,'BackgroundColor','white');
    end

Would using GUIDATA instead of get/set be a better option? New to GUIDE and Matlab, so any explanation would be extremely helpful.

Edit--Full error message:

Reference to non-existent field 'group_variable_popupmenu'.

Error in mockup>choose_data_button_Callback (line 148)
set(handles.group_variable_popupmenu, 'string', data(1,:));

Error in gui_mainfcn (line 95)
    feval(varargin{:});

Error in mockup (line 42)
gui_mainfcn(gui_State, varargin{:});

Error in @(hObject,eventdata)mockup('choose_data_button_Callback',hObject,eventdata,guidata(hObject))

Error while evaluating UIControl Callback
2
With the provided code there is no reason why this wouldn't work. Have you made changes to the handles structure elsewhere in the file? What is the full error message? - excaza
Haven't changed the handles structure that I know of. Only time I've used it is for gets/sets. - Nemophilia
Time to learn how to debug. Use MATLAB's debugger and pause execution at the line that creates the error. Look at the field names in handles and see if group_variable_popupmenu is one of them. - excaza

2 Answers

0
votes

I had some issues regarding this error in one of my code recently as well, and i had the same trouble as you to find an appropriate answer, which i only found by myself at the end.

My issue was that it would not find the field for one of my figure once i entered one of my callback function.

The solution i found was to move the creation of my figure inside the callback function, and not outside.

Maybe try to do the same by moving your popup menu creation at the beginning of your callback function, or something along thoses lines.

0
votes

This might be trivial, but are you running the GUI Figure from either guide or the command window? One mistake I did many times at first was to just double click the .fig file in the folder menu (which does not call the constructor), instead of calling it from the command window by typing in the name of the .fig file (or via guide, as an alternative).