Harrison
Harrison

Reputation: 11

How do you load a file whose file name is stored in an array?

here is my code:

function [] = plotavg (x)
files = dir('*.mat');
for c=1:length(files)
    load files(c);
    d=0;
    if start_month == x
        for i=1:length(data)
            d = d + data(i);
        end
    end
end

I don't know how to write it so that the load function loads the file listed in that index of the array

Thanks!

Upvotes: 0

Views: 155

Answers (1)

Felix Darvas
Felix Darvas

Reputation: 507

dir returns a struct with some field names. One of them is "name", i.e. the name of the file. If you type e.g. files(1) in the console, you will see the fields you get for each item in your directory.

Replace

load files(c);

with

load(files(c).name);

and it should work.

Upvotes: 5

Related Questions