Hello All,
I am trying to run my code over sub folder but facing issues. I am trying to extrac perticular variable from multiple .mat files.
I could do that for 1 folder having multiple .mat files with following code:
clear allclose alld = uigetdir();filePattern = fullfile(d, '*.mat');file = dir(filePattern);x = cell(1, numel(file));for k = 1: numel(file) baseFileName = file(k).name; fullFileName = fullfile(d, baseFileName); fprintf('Now Reading file %s\n', fullFileName); x{k}=load(fullFileName,'Veriable1'); j{k}=cell2mat(struct2cell(x{k})); endvar1=cat(1,j{:});
Now I am trying to run this over the loop for subfolder and now facing issues and need help
Here is my current try:
clear allclose allD = 'myPath';S = dir(fullfile(D,'*'));N = setdiff({S([S.isdir]).name},{'.','..'}); % list of subfolders of D.
for ii = 1:numel(N) T = dir(fullfile(D,N{ii},'*.mat')); % improve by specifying the file extension.
C = {T(~[T.isdir]).name}; % files in subfolder.
for jj = 1:numel(C) filePattern = fullfile(D,N{ii},C{jj}) %filePattern = fullfile(F, '*.mat');
file = dir(filePattern); x = cell(1, numel(file)); for k = 1: numel(file) baseFileName = file(k).name; fullFileName = fullfile(D,N{ii}, baseFileName); fprintf('Now Reading file %s\n', fullFileName); x{k}=load(fullFileName,'Veriable1'); j{k}=cell2mat(struct2cell(x{k})); end endendvar1=cat(1,j{:});
I know it has many flaws but I am not at all able to think further and need help to make it work.
Best Answer