Search code examples
matlabmatlab-struct

Reference to non-existent field when using variable for struct access


I'm very new to Matlab and maybe a very basic question, but I need help:

I have a path:

file = C:\this\is\path\to\my_file.mat

This is a file which contains nested structure and I need just to access one variable. In my script, first I need to get the file name which I will use as variable later:

[pathstr,name,ext] = fileparts(file);

So, now I have the variable name with the content my_file.mat

S = load(file)

value = S.('name').structA.SubstructA1.variableA

Matlab is generating the message:

"Reference to non-existent field 'name'."

So, how is the correct way to use the variable in this case ?


Solution

  • You are very close, just remove the single quotes from around the variable, name, like this:

    value = S.(name).structA.SubstructA1.variableA