Access data in different files stored in different folders
I've searched around and found some potential solutions for my problem but have been unable to implement the code.
Essentially, I have one directory with 32 sub-folders. Each of the 32 sub-folders has 4 files inside (.mat with 1 row and a few million columns each). My variable of interest is called data (see bellow in code).
I need to access all 4 .mat files inside a subset of the sub-folders and append/concatenate them into a single big matrix. More, every group of 4 files in every sub-folder should be next to each other in the end matrix.
Also, the names of the sub-folders and the files within are known:
Folders = TT1, TT2, etc.
Files = TT1ch1, TT1ch2, TT1ch3, TT1ch4; TT2ch1, TT2ch2, TT2ch3, TT2ch4, etc.
I would also need to specify in the code which sub-folders to actually open and read the 4 files from. Not all need to be read at all times. Until now I have this:
TTs = [1,2,3,4,5]; % List of sub-folders to use.
for i = TTs;
addpath(strcat('TT',num2str(i)));
cd (strcat('TT',num2str(i)));
for w = 1:4; %get data from the 4 files
load(strcat('TT',num2str(i),'ch', num2str(w), '.mat'));
allChs(w,:) = data(1,:); %concatenate into one matrix
end
cd ..
rmpath(strcat('TT',num2str(i)));
end
With this code I'm able to read the data from 4 files of a given sub-folder and copy it to a new matrix (allChs). Yet, when I try to add code to go through all folders I simply overwrite what I have...
I've tried different things but am quite stuck at this stage. Any help would be dearly welcome.
Cheers,
Oiko
matlab for-loop signal-processing
add a comment |
I've searched around and found some potential solutions for my problem but have been unable to implement the code.
Essentially, I have one directory with 32 sub-folders. Each of the 32 sub-folders has 4 files inside (.mat with 1 row and a few million columns each). My variable of interest is called data (see bellow in code).
I need to access all 4 .mat files inside a subset of the sub-folders and append/concatenate them into a single big matrix. More, every group of 4 files in every sub-folder should be next to each other in the end matrix.
Also, the names of the sub-folders and the files within are known:
Folders = TT1, TT2, etc.
Files = TT1ch1, TT1ch2, TT1ch3, TT1ch4; TT2ch1, TT2ch2, TT2ch3, TT2ch4, etc.
I would also need to specify in the code which sub-folders to actually open and read the 4 files from. Not all need to be read at all times. Until now I have this:
TTs = [1,2,3,4,5]; % List of sub-folders to use.
for i = TTs;
addpath(strcat('TT',num2str(i)));
cd (strcat('TT',num2str(i)));
for w = 1:4; %get data from the 4 files
load(strcat('TT',num2str(i),'ch', num2str(w), '.mat'));
allChs(w,:) = data(1,:); %concatenate into one matrix
end
cd ..
rmpath(strcat('TT',num2str(i)));
end
With this code I'm able to read the data from 4 files of a given sub-folder and copy it to a new matrix (allChs). Yet, when I try to add code to go through all folders I simply overwrite what I have...
I've tried different things but am quite stuck at this stage. Any help would be dearly welcome.
Cheers,
Oiko
matlab for-loop signal-processing
You don’t needaddpath
andrmpath
there. You are always reading from the current folder, the path is not used.
– Cris Luengo
Nov 11 '18 at 12:53
Thanks Cris. Just removed those lines from the code as they are unecessary. Cheers
– Oiko
Nov 11 '18 at 22:38
add a comment |
I've searched around and found some potential solutions for my problem but have been unable to implement the code.
Essentially, I have one directory with 32 sub-folders. Each of the 32 sub-folders has 4 files inside (.mat with 1 row and a few million columns each). My variable of interest is called data (see bellow in code).
I need to access all 4 .mat files inside a subset of the sub-folders and append/concatenate them into a single big matrix. More, every group of 4 files in every sub-folder should be next to each other in the end matrix.
Also, the names of the sub-folders and the files within are known:
Folders = TT1, TT2, etc.
Files = TT1ch1, TT1ch2, TT1ch3, TT1ch4; TT2ch1, TT2ch2, TT2ch3, TT2ch4, etc.
I would also need to specify in the code which sub-folders to actually open and read the 4 files from. Not all need to be read at all times. Until now I have this:
TTs = [1,2,3,4,5]; % List of sub-folders to use.
for i = TTs;
addpath(strcat('TT',num2str(i)));
cd (strcat('TT',num2str(i)));
for w = 1:4; %get data from the 4 files
load(strcat('TT',num2str(i),'ch', num2str(w), '.mat'));
allChs(w,:) = data(1,:); %concatenate into one matrix
end
cd ..
rmpath(strcat('TT',num2str(i)));
end
With this code I'm able to read the data from 4 files of a given sub-folder and copy it to a new matrix (allChs). Yet, when I try to add code to go through all folders I simply overwrite what I have...
I've tried different things but am quite stuck at this stage. Any help would be dearly welcome.
Cheers,
Oiko
matlab for-loop signal-processing
I've searched around and found some potential solutions for my problem but have been unable to implement the code.
Essentially, I have one directory with 32 sub-folders. Each of the 32 sub-folders has 4 files inside (.mat with 1 row and a few million columns each). My variable of interest is called data (see bellow in code).
I need to access all 4 .mat files inside a subset of the sub-folders and append/concatenate them into a single big matrix. More, every group of 4 files in every sub-folder should be next to each other in the end matrix.
Also, the names of the sub-folders and the files within are known:
Folders = TT1, TT2, etc.
Files = TT1ch1, TT1ch2, TT1ch3, TT1ch4; TT2ch1, TT2ch2, TT2ch3, TT2ch4, etc.
I would also need to specify in the code which sub-folders to actually open and read the 4 files from. Not all need to be read at all times. Until now I have this:
TTs = [1,2,3,4,5]; % List of sub-folders to use.
for i = TTs;
addpath(strcat('TT',num2str(i)));
cd (strcat('TT',num2str(i)));
for w = 1:4; %get data from the 4 files
load(strcat('TT',num2str(i),'ch', num2str(w), '.mat'));
allChs(w,:) = data(1,:); %concatenate into one matrix
end
cd ..
rmpath(strcat('TT',num2str(i)));
end
With this code I'm able to read the data from 4 files of a given sub-folder and copy it to a new matrix (allChs). Yet, when I try to add code to go through all folders I simply overwrite what I have...
I've tried different things but am quite stuck at this stage. Any help would be dearly welcome.
Cheers,
Oiko
matlab for-loop signal-processing
matlab for-loop signal-processing
asked Nov 11 '18 at 12:41
OikoOiko
83
83
You don’t needaddpath
andrmpath
there. You are always reading from the current folder, the path is not used.
– Cris Luengo
Nov 11 '18 at 12:53
Thanks Cris. Just removed those lines from the code as they are unecessary. Cheers
– Oiko
Nov 11 '18 at 22:38
add a comment |
You don’t needaddpath
andrmpath
there. You are always reading from the current folder, the path is not used.
– Cris Luengo
Nov 11 '18 at 12:53
Thanks Cris. Just removed those lines from the code as they are unecessary. Cheers
– Oiko
Nov 11 '18 at 22:38
You don’t need
addpath
and rmpath
there. You are always reading from the current folder, the path is not used.– Cris Luengo
Nov 11 '18 at 12:53
You don’t need
addpath
and rmpath
there. You are always reading from the current folder, the path is not used.– Cris Luengo
Nov 11 '18 at 12:53
Thanks Cris. Just removed those lines from the code as they are unecessary. Cheers
– Oiko
Nov 11 '18 at 22:38
Thanks Cris. Just removed those lines from the code as they are unecessary. Cheers
– Oiko
Nov 11 '18 at 22:38
add a comment |
1 Answer
1
active
oldest
votes
As @Cris Luengo said, you don't need add to path a folder for reading from it.
Also, you don't need cd
, you better explicit the path you want to read from:
parentPath = <your-main-folder>;
TTs = [1,2,3,4,5]; % List of sub-folders to use.
Now, all you need is to move on with lines as you move on with folders, so that it will not override on the next sub-folder:
for k = TTs;
for w = 1:4; %get data from the 4 files
load(fullfile(parentPath ,strcat('TT',num2str(i),'ch', num2str(w), '.mat')));
allChs(4*(k-1)+w,:) = data(1,:); %concatenate into one matrix
end
end
Thanks Adiel, this did the trick! Cheers
– Oiko
Nov 11 '18 at 22:37
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53248853%2faccess-data-in-different-files-stored-in-different-folders%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
As @Cris Luengo said, you don't need add to path a folder for reading from it.
Also, you don't need cd
, you better explicit the path you want to read from:
parentPath = <your-main-folder>;
TTs = [1,2,3,4,5]; % List of sub-folders to use.
Now, all you need is to move on with lines as you move on with folders, so that it will not override on the next sub-folder:
for k = TTs;
for w = 1:4; %get data from the 4 files
load(fullfile(parentPath ,strcat('TT',num2str(i),'ch', num2str(w), '.mat')));
allChs(4*(k-1)+w,:) = data(1,:); %concatenate into one matrix
end
end
Thanks Adiel, this did the trick! Cheers
– Oiko
Nov 11 '18 at 22:37
add a comment |
As @Cris Luengo said, you don't need add to path a folder for reading from it.
Also, you don't need cd
, you better explicit the path you want to read from:
parentPath = <your-main-folder>;
TTs = [1,2,3,4,5]; % List of sub-folders to use.
Now, all you need is to move on with lines as you move on with folders, so that it will not override on the next sub-folder:
for k = TTs;
for w = 1:4; %get data from the 4 files
load(fullfile(parentPath ,strcat('TT',num2str(i),'ch', num2str(w), '.mat')));
allChs(4*(k-1)+w,:) = data(1,:); %concatenate into one matrix
end
end
Thanks Adiel, this did the trick! Cheers
– Oiko
Nov 11 '18 at 22:37
add a comment |
As @Cris Luengo said, you don't need add to path a folder for reading from it.
Also, you don't need cd
, you better explicit the path you want to read from:
parentPath = <your-main-folder>;
TTs = [1,2,3,4,5]; % List of sub-folders to use.
Now, all you need is to move on with lines as you move on with folders, so that it will not override on the next sub-folder:
for k = TTs;
for w = 1:4; %get data from the 4 files
load(fullfile(parentPath ,strcat('TT',num2str(i),'ch', num2str(w), '.mat')));
allChs(4*(k-1)+w,:) = data(1,:); %concatenate into one matrix
end
end
As @Cris Luengo said, you don't need add to path a folder for reading from it.
Also, you don't need cd
, you better explicit the path you want to read from:
parentPath = <your-main-folder>;
TTs = [1,2,3,4,5]; % List of sub-folders to use.
Now, all you need is to move on with lines as you move on with folders, so that it will not override on the next sub-folder:
for k = TTs;
for w = 1:4; %get data from the 4 files
load(fullfile(parentPath ,strcat('TT',num2str(i),'ch', num2str(w), '.mat')));
allChs(4*(k-1)+w,:) = data(1,:); %concatenate into one matrix
end
end
answered Nov 11 '18 at 14:26
AdielAdiel
2,6921020
2,6921020
Thanks Adiel, this did the trick! Cheers
– Oiko
Nov 11 '18 at 22:37
add a comment |
Thanks Adiel, this did the trick! Cheers
– Oiko
Nov 11 '18 at 22:37
Thanks Adiel, this did the trick! Cheers
– Oiko
Nov 11 '18 at 22:37
Thanks Adiel, this did the trick! Cheers
– Oiko
Nov 11 '18 at 22:37
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53248853%2faccess-data-in-different-files-stored-in-different-folders%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You don’t need
addpath
andrmpath
there. You are always reading from the current folder, the path is not used.– Cris Luengo
Nov 11 '18 at 12:53
Thanks Cris. Just removed those lines from the code as they are unecessary. Cheers
– Oiko
Nov 11 '18 at 22:38