Access data in different files stored in different folders









up vote
1
down vote

favorite












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










share|improve this question





















  • 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 at 12:53










  • Thanks Cris. Just removed those lines from the code as they are unecessary. Cheers
    – Oiko
    Nov 11 at 22:38















up vote
1
down vote

favorite












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










share|improve this question





















  • 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 at 12:53










  • Thanks Cris. Just removed those lines from the code as they are unecessary. Cheers
    – Oiko
    Nov 11 at 22:38













up vote
1
down vote

favorite









up vote
1
down vote

favorite











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










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 12:41









Oiko

83




83











  • 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 at 12:53










  • Thanks Cris. Just removed those lines from the code as they are unecessary. Cheers
    – Oiko
    Nov 11 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 at 12:53










  • Thanks Cris. Just removed those lines from the code as they are unecessary. Cheers
    – Oiko
    Nov 11 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 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 at 12:53












Thanks Cris. Just removed those lines from the code as they are unecessary. Cheers
– Oiko
Nov 11 at 22:38





Thanks Cris. Just removed those lines from the code as they are unecessary. Cheers
– Oiko
Nov 11 at 22:38













1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










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





share|improve this answer




















  • Thanks Adiel, this did the trick! Cheers
    – Oiko
    Nov 11 at 22:37











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',
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
);



);













draft saved

draft discarded


















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








up vote
1
down vote



accepted










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





share|improve this answer




















  • Thanks Adiel, this did the trick! Cheers
    – Oiko
    Nov 11 at 22:37















up vote
1
down vote



accepted










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





share|improve this answer




















  • Thanks Adiel, this did the trick! Cheers
    – Oiko
    Nov 11 at 22:37













up vote
1
down vote



accepted







up vote
1
down vote



accepted






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





share|improve this answer












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






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 at 14:26









Adiel

2,667920




2,667920











  • Thanks Adiel, this did the trick! Cheers
    – Oiko
    Nov 11 at 22:37

















  • Thanks Adiel, this did the trick! Cheers
    – Oiko
    Nov 11 at 22:37
















Thanks Adiel, this did the trick! Cheers
– Oiko
Nov 11 at 22:37





Thanks Adiel, this did the trick! Cheers
– Oiko
Nov 11 at 22:37


















draft saved

draft discarded
















































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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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.




draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

Barbados

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3