Sort files by name inside a folder

Dear Matlab community. I have a dilemma at hand. I am working with a dataset from an accelerometer to develop a classification algorithm. My data was subdivided into folders with CSV files of running, walking up and down the stairs, walking. Each document has 4 columns: timestamp, X, Y and Z values. Since I couldn't read each folder separately, I joined all the activities' CVS files into one single folder called Walking_dun. I managed to create a loop and read all the CVS files. Here is my code: %
% Reading the path file
cd = 'C:\users\adria\MATLAB Drive\';
pathname = 'C:\users\adria\MATLAB Drive \Walking_dun';
fileList = dir('*.csv*')
numberOfFiles = length(fileList)
data = numberOfFiles
for i = 1:numberOfFiles
fileName = fileList(i).name;
table = readtable(fileName,'Format','auto');
table = table(101:end-100,:);
x = table.("x")
y = table.("y")
z = table.("z")
With the loop, I am able to read all the CSV files in the walking_dun folder. I have specified, for example, walking down the stairs as WD and walking normal as WN, as you can see in the image. I want to extract features from each channel. However, how can I differentiate between the different activities ( Running, walking down the stairs, up the stairs, walking ) if, as you can see, I get all the x, y and z, but I don't know how to group them into the activities. I need to be able to do so because if I want to join all the calculated features from all the x, y and z channels into a table to be able to train a model, I can't identify which is which.
So, is there a way of grouping the files by name (activity) after being read on the loop, such as, for example, grouping all WD.csv together so that when I calculate the features, they can be placed into a matrix table? Hence my question is there a way that instead of my having all the cvs sheets in one folder, can they be subdivided into a folder, and those folders attribute the category or, in this case, the activity. Is there a more efficient approach to this problem? I feel I am overcomplicating this. I appreciate all your feedback.
Thank you for your time

7 件のコメント

Stephen23
Stephen23 2022 年 5 月 3 日
編集済み: Stephen23 2022 年 5 月 3 日
"Since I couldn't read each folder separately..."
Why not? You can easily use DIR to loop over subfolders, e.g.:
P = 'C:\users\adria\MATLAB Drive';
S = dir(fullfile(P,'*','*.csv'));
I get the feeling that merging the files into one folder might not have helped you. However you omitted to tell us some useful info, e..g if those subfolders corresponded to the topics that you want to group by, or what the subfolder names are.
" I have specified, for example, walking down the stairs as WD and walking normal as WN..."
Did you change the filenames in order to merge them into one folder? Hopefully not.
"is there a way of grouping the files by name (activity) after being read on the loop, such as, for example, grouping all WD.csv together..."
Of course, e.g. use CONTAINS on the filenames or call DIR with a search string that matches only the required filenames.
Sarah DS
Sarah DS 2022 年 5 月 3 日
Hello Stephen, thank you for your feedback. I have tried the code you just gave me. The S appears as an empty array, for some reason it does not recognised the folder within the main folder. I am using Matlab online.
Yes, the file names: running, walking up the stairs, walking down the stairs and walking normal, the categories I want to classify.
Stephen23
Stephen23 2022 年 5 月 3 日
編集済み: Stephen23 2022 年 5 月 3 日
"I have tried the code you just gave me."
Hopefully by modifying it to suit the path to the subfolders, which you did not tell us.
"The S appears as an empty array, for some reason it does not recognised the folder within the main folder."
The intent of that code was to loop over all of the original subfolders, not just one "the folder" as you write.
"I am using Matlab online."
I have no experience with that. Lets see if DIR will find all CSV files in some subfolders:
mkdir('A')
writematrix(11,'A/A1.csv')
writematrix(12,'A/A2.csv')
mkdir('B')
writematrix(21,'B/B1.csv')
S = dir(fullfile('.','*','*.csv'))
S = 3×1 struct array with fields:
name folder date bytes isdir datenum
{S.name}
ans = 1×3 cell array
{'A1.csv'} {'A2.csv'} {'B1.csv'}
{S.folder}
ans = 1×3 cell array
{'/users/mss.system.Fg7mjO/A'} {'/users/mss.system.Fg7mjO/A'} {'/users/mss.system.Fg7mjO/B'}
So far everything seems to be worknig as expected.
"Yes, the file names: running, walking up the stairs, walking down the stairs and walking normal, the categories I want to classify."
I asked about the subfolder names. Please give the exact names of the subfolders. And the path to their parent directory.
Sarah DS
Sarah DS 2022 年 5 月 3 日
I do apologise, the subfolder names are : running, walking up the stairs, walking down the stairs and walking normal. Within each sub folder I have 10 cvs sheets.
Stephen23
Stephen23 2022 年 5 月 3 日
編集済み: Stephen23 2022 年 5 月 3 日
Please run this command:
dir C:\users\adria\MATLAB Drive
and paste its output in a new comment.
Sarah DS
Sarah DS 2022 年 5 月 3 日
Thank you sir! May I ask you, how can I avoid the table variable to be overwritten? Because when I print the variable, only the last csv table is stored. In what way can I prevent this from happening?
Stephen23
Stephen23 2022 年 5 月 3 日
"how can I avoid the table variable to be overwritten?"
Use indexing, e.g.:
P = 'absolute or relative path to where the subfolder are';
S = dir(fullfile(P,'*','*.csv'));
for k = 1:numel(S)
F = fullfile(S(k).folder,S(k).name);
S(k).data = readtable(F,'Format','auto');
end % ^ indexing to store the imported data
Your import data are stored in the structure S. For example, for the second file:
S(2).folder % path
S(2).name % filename
S(2).data % imported data

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

回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeFile Operations についてさらに検索

製品

リリース

R2022a

質問済み:

2022 年 5 月 2 日

コメント済み:

2022 年 5 月 3 日

Community Treasure Hunt

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

Start Hunting!

Translated by