basickarl
basickarl

Reputation: 40561

MatLab load file from relative path

I have file my_file.m in the directory C:\...\mydir\, does not matter which. I have data I wish to load C:\...\mydir\anotherdir\ which contains my files I wish to load. The following does not seem to work.

files = dir('anotherdir\*.mat');

I want to load files which are relative to the my_file.m which is requesting them.

Answer: To load it further use the following:

current_dir = pwd;
files = dir([current_dir '\anotherdir\*.mat']);
dname=[current_dir '\anotherdir\'];

for i=1:length(files)
    fname=fullfile(dname,files(i).name);
    A = load(fname);
end

Upvotes: 1

Views: 6611

Answers (1)

am304
am304

Reputation: 13886

You could do:

current_dir = pwd;
files = dir([current_dir '\anotherdir\*.mat']);

Or simply use cd as suggested in the comments

Upvotes: 1

Related Questions