Sort files by name inside a folder

11 visualizaciones (últimos 30 días)
Sarah DS
Sarah DS el 2 de Mayo de 2022
Comentada: Stephen23 el 3 de Mayo de 2022
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 comentarios
Sarah DS
Sarah DS el 3 de Mayo de 2022
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 el 3 de Mayo de 2022
"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

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre File Operations en Help Center y File Exchange.

Productos


Versión

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by