Search code examples
matlabuser-interfacestructmatlab-load

MATLAB loading .mat files from gui and accessing variables inside the struct


I'm designing a gui and I am trying to load a mat file into a struct and then access data from the struct. I also want to print the variables in the struct to see if the variables are in the mat file and they work. Thanks, I also don't have much experience in matlab. Here is my code:

function pushbutton5_Callback(hObject, eventdata, handles)
%get path
[FileName,PathName] = uigetfile('*.mat','Select mat file');

if (FileName==0) %cancel is pressed
    return;
end

Struct1 = load('FileName');
Structname = fieldnames(Sruct1);


MatDef = [PathName FileName]; %path and name
set(handles.edit2,'String',MatDef) %shows directory string

Right now I am getting errors trying to load the mat file into a struct.


Solution

  • Try replacing with the following:

    [FileName,PathName] = uigetfile('*.mat','Select mat file');
    if FileName==0, return, end
    
    Struct1 = load( fullfile(PathName,FileName) );   %# pass file path as string
    Structname = fieldnames(Struct1);                %# typo?