Joel Wong
Joel Wong

Reputation: 69

Error when trying to access a field in MATLAB structure

I am trying to run a loop over multiple files, by feeding the file names into a function. I have saved these file names as a structure using:

files=dir('testdata\*.siz');
nrows=size(files,1);

Now my loop is:

for i=1:nrows
filename=files.name{i};
Singapore(filename);
writetable(ans,'file.xls')
end

However, I get the error:
"Field reference for multiple structure elements that is followed by more reference blocks is an error."

I found that the error is in

filename=files.name{1};

but everywhere I've searched tells me to use { } to access fields in a structure. I also have tried other types of brackets in vain.

Additional Information:
'files' is the name of the structure
'name' is the first column field within 'files' containing the file names in inverted commas.

Upvotes: 0

Views: 94

Answers (1)

matlabgui
matlabgui

Reputation: 5672

You are referencing the files struct wrong, you need:

files(i).name

The {} is for accessing cell arrays.

You should also use ii (or similar) instead of i as your indexing variable since i is a already Matlab variable (imaginary unit).

Upvotes: 2

Related Questions