Save data from subfolders into a cell array
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
Hi all,
I am trying to access several matrices from my directiry and store them in a cell array.
They are stored in a way where I have 1062 subfolders in my directory that are named with the subject ID. Each of that subfolder contains 4 individual .csv files that are all named the exact same across all subjects.
What I would like to do now is go into each of the subfolder, extract one of the 4 csv.files and save it as a matrix in a cell array. Optimally, the information to which subfolder (aka subject) the matrix belongs would also be included in the cell array.
Any help would be appreciated!
Thanks
採用された回答
P = 'absolute or relative path to where the subfolders are';
N = 'the name of the file that you want.CSV';
S = dir(fullfile(P,'*'));
S = S([S.isdir]); % remove files
S(ismember({S.name},{'.','..'})) = []; % remove dot directories
for k = 1:numel(S) % loop over the subfolders
F = fullfile(P,S(k).name,N);
S(k).data = readmatrix(F);
end
The imported data are simply stored in the structure S. For example the second file:
S(2).name % subject ID
S(2).data % imported file data
You can trivially loop over the structure and use indexing to process all of the file data.
8 件のコメント
Johanna Popp
2022 年 2 月 4 日
Thanks a lot for your response!
I tried your approach but I got this error message in the line to remove the dot directories:
Input A of class double and input B of class cell must be cell arrays of character vectors, unless one is a character vector.
@Johanna Popp: I used the wrong type of brackets, that line should be:
S(ismember({S.name},{'.','..'})) = [];
% ^ ^ curly braces
I have corrected my answer. If you are interested how that syntax works, see:
Johanna Popp
2022 年 2 月 4 日
Ah you fixed the brackets, thanks!
The structure is still empty but I think I know where the issue is: I forgot to include in my question that the subject specific folder contains ANOTHER subfolder (named the same exact same for all subjects) that then contains my 4 csv files.. It would be great if you could show a way to include this in the solution, thanks!!
"The structure is still empty..."
If you mean that
numel(S)
is zero then no subfolders exist on the path that you gave. Check the path carefully.
"I forgot to include in my question that the subject specific folder contains ANOTHER subfolder (named the same exact same for all subjects) that then contains my 4 csv files.. It would be great if you could show a way to include this in the solution, thanks!!"
You wll need to include the name of the subsubfolder inside the FULLFILE inside the loop, e.g.:
D = 'the name of the subsubfolder';
..
for k = ..
F = fullfile(P,S(k).name,D,N);
% ^
end
Look at the order I used inside FULLFILE:
- the path to where the subfolders are (constant)
- the subfolder name (subject ID)
- the subsubfolder name (constant)
- the filename (constant)
This is exactly the same as if you were writing the filepath down yourself. Have a look at the output of FULLFIlE (i.e. variable F), it should match what you would see in your explorer address bar or similar tool. You can adjust the folder/file names and order to suit your folder structure.
Johanna Popp
2022 年 2 月 4 日
Thank you so much for your help!!
Johanna Popp
2022 年 2 月 4 日
Perfekt, thank you for the explanations!!
Say one of the subjects doesn't have a data entry because the specific csv file that I am looking for is not in the folder. Is the a way to skip subjects?
thanks :)
"Is the a way to skip subjects? "
You can test if a particular folder of file exists:
and add an IF condition inside the loop when the file data is imported, e.g.:
if isfile(F)
S(k).data = readmatrix(F);
end
You can do something similar for the subsubfolders too, if required.
Johanna Popp
2022 年 2 月 4 日
awesome, thanks!
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で File Operations についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
