handling data inside for loop
1 回表示 (過去 30 日間)
古いコメントを表示
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

0 件のコメント
採用された回答
Matt J
2024 年 11 月 24 日
編集済み: Matt J
2024 年 11 月 25 日
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 件のコメント
Matt J
2024 年 11 月 25 日
編集済み: Matt J
2024 年 11 月 25 日
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
2024 年 11 月 25 日
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{:})
filename = 'tempfile.csv';
writecell(G, filename);
dbtype(filename)
So your existing code does separate original blocks.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Matrix Indexing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!