Pulling .mat files from multiple directories in one function

2 ビュー (過去 30 日間)
Mary
Mary 2013 年 6 月 11 日
I'm writing a function that pulls data from one directory, compares it's string name to another file in another directory and then runs processes on them both
So far things look like this
function matlabanswers_plzhelp
eData= dir('C:/file/file/file/*.mat')
for i = 1:numel(eData)
eName = eData.name
a=regexp(eName,'_','split');
experimentName = a{1};
load(eName)
lData = dir('C:/differentfolder/*.mat')
for n=1:numel(lData)
lName = lData.name
if strcmp(lName,experimentName) ==1
load lName
% run code using variables from both loaded eName and lName.mat files
else
end
end
end
For some reason this type of code works when I'm working out of one directory, but refuses to actually load the files if they are from different directories...
any idea of what I'm doing wrong?
Thanks ML

採用された回答

Jonathan Sullivan
Jonathan Sullivan 2013 年 6 月 11 日
When you call load, you are not referencing them by their file name only (i.e. no directory). Try something like this:
function matlabanswers_plzhelp
d1 = ['C:/file/file/file/'];
eData= dir([d1 '*.mat'])
for i = 1:numel(eData)
eName = eData.name
a=regexp(eName,'_','split');
experimentName = a{1};
load([d1 eName])
d2 = 'C:/differentfolder/';
lData = dir([d2 '*.mat'])
for n=1:numel(lData)
lName = lData.name
if strcmp(lName,experimentName) ==1
load([d2 lName])
% run code using variables from both loaded eName and lName.mat files
else
end
end

その他の回答 (1 件)

Kevin Claytor
Kevin Claytor 2013 年 6 月 11 日
編集済み: Kevin Claytor 2013 年 6 月 11 日
Windows convention with 'nix file slash...?
Anyway... when you call; "load lName" it searches for lName from the current directory. Try;
lDir = 'C:\differentfolder\'
lData = dir([lDir,*.mat])
...
if strcmp(lName,experimentName) ==1
load(fullfile(lDir,lName))
  1 件のコメント
Mary
Mary 2013 年 6 月 12 日
Oops, that's what I get for not copy/pasting my real code! (I like using junk code when I request answers so that I can go through line by line and learn what the answer does when retyping it into my real code)
Sorry for the \ instead of /!
Thank you - I'm still getting code errors (for other things) - but I think that this solved the directory problem!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by