handling data inside for loop

2 views (last 30 days)
Rita Campos
Rita Campos on 24 Nov 2024
Commented: Walter Roberson on 25 Nov 2024
Hi, I am trying to read information from several .csv files located inside a folder.
As those files are read by Matlab, I need to add a line after the data for each .csv file so I can distinguish what data is from which file.
I need to turn that data into a table to be able to process it after.
I'm getting stuck in the line where it is storing the data in cell array:
%TS_R_SS
folderPath4 = '03TS-all_time_domain\Relaxing\SS';
% Get a list of all CSV files in the folder
filePattern4 = fullfile(folderPath4, '*.csv');
csvFiles4 = dir(filePattern4);
% Initialize a cell array to hold the data from each file
dataAllFiles4 = cell(length(csvFiles4)*2, 1);% Extra space for empty lines
% Loop through each file and read the data
dataIndex = 1
for k4 = 1:length(csvFiles4)
% Get the full file name
baseFileName4 = csvFiles4(k4).name;
fullFileName4 = fullfile(folderPath4, baseFileName4);
% Read the data from the CSV file
dados4 = readtable(fullFileName4);
% Store the data in the cell array
dataAllFiles4(dataIndex:dataIndex + height(dados4) - 1) = table2cell(dados4);
% Update the index for the next data entry
dataIndex = dataIndex + height(dados4);
% Add an empty line after each file's data
dataAllFiles4{dataIndex} = []; % Insert an empty cell
dataIndex = dataIndex + 1; % Move to the next index
end
The error message it shows is:
Unable to perform assignment because the left and right sides have a
different number of elements.
Error in Extract_audio_features_Neurokit (line 165)
dataAllFiles4(dataIndex:dataIndex + height(dados4) - 1) =
table2cell(dados4);
How can I solve this please?
Thanks

Accepted Answer

Matt J
Matt J on 24 Nov 2024
Edited: Matt J on 25 Nov 2024
dataAllFiles4 = cell(2,length(csvFiles4));
dataIndex = 1
for k4 = 1:length(csvFiles4)
% Get the full file name
baseFileName4 = csvFiles4(k4).name;
fullFileName4 = fullfile(folderPath4, baseFileName4);
% Read the data from the CSV file
dados4 = readtable(fullFileName4);
% Store the data in the cell array
C= table2cell(dados4); w=width(C);
dataAllFiles4{1,i} = C;
dataAllFiles4{2,i} = repmat( {[]} , 1,w );
end
dataAllFiles4=vertcat(dataAllFiles4{:});
  4 Comments
Matt J
Matt J on 25 Nov 2024
Edited: Matt J on 25 Nov 2024
Could you help me further to solve the issue with the lack of a blank row between data sets from each csv file and also how to preserve the headers?
Your original code converts everything to cell arrays so I don't know what it means to "preserve the headers" (cell arrays don't have headers).You can certainly create an additional cell array to hold the headers, if that's what you mean:
dataAllFiles4 = cell(2,length(csvFiles4));
Headers= dataAllFiles4(1,:);
for k4 = 1:length(csvFiles4)
% Get the full file name
baseFileName4 = csvFiles4(k4).name;
fullFileName4 = fullfile(folderPath4, baseFileName4);
% Read the data from the CSV file
dados4 = readtable(fullFileName4);
% Store the data in the cell array
C= table2cell(dados4); w=width(C);
dataAllFiles4{1,i} = C;
dataAllFiles4{2,i} = repmat( {[]} , 1,w );
Headers{i}=dados4.Properties.VariableNames;
end
dataAllFiles4=vertcat(dataAllFiles4{:});
Walter Roberson
Walter Roberson on 25 Nov 2024
C = {'a', 1; 'b', 2};
w=width(C);
i = 1;
dataAllFiles4{1,i} = C;
dataAllFiles4{2,i} = repmat( {[]} , 1,w );
C = {'c', 3; 'd', 4};
w=width(C);
i = 2;
dataAllFiles4{1,i} = C;
dataAllFiles4{2,i} = repmat( {[]} , 1,w );
G = vertcat(dataAllFiles4{:})
G = 6x2 cell array
{'a' } {[ 1]} {'b' } {[ 2]} {0x0 double} {0x0 double} {'c' } {[ 3]} {'d' } {[ 4]} {0x0 double} {0x0 double}
filename = 'tempfile.csv';
writecell(G, filename);
dbtype(filename)
1 a,1 2 b,2 3 , 4 c,3 5 d,4 6 ,
So your existing code does separate original blocks.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!