Size in byte of file reduced when i combine two csv
1 次查看(过去 30 天)
显示 更早的评论
Here is code that can first check the length of file if it is less then 800 it will do following steps first it will get the data from 1-600 rows in a Variable then using same csv it will get 1-200 rows in a Variable save both variable in new files and at the end combine them but issue is that my file size in byte is reduced when ever i check the data in cell i observe that values are truncated. can any one have another logic for this work
home_dir = 'D:\DS\Testing';
sensors = dir(home_dir);
for sens =3:length(sensors)
classes = dir(fullfile(home_dir, sensors(sens).name));
for cls=3:length(classes)
data = dir(fullfile(home_dir, sensors(sens).name, classes(cls).name));
toWrite = cell(zeros(1,1));
for dat = 3:length(data)
tmp = readtable(fullfile(home_dir, sensors(sens).name, classes(cls).name, data(dat).name));
n_rows_target = 800;
n_rows = size(tmp,1);
if n_rows < n_rows_target % Check the file length
B=tmp(1:600,1:end); %Get the data from 1-600 rows 1-4 Columns
C=tmp(1:200,1:end); % Get the data from 1-200 rows 1-4 Columns same file
writetable(B, 'new.csv'); % File is created in which 1-600 rows exist
writetable(C,'Two.csv'); % File is created in which 1-200 rows chunk
csv1 = readmatrix('new.csv'); % Reading Files
csv2 = readmatrix('Two.csv');
allCsv = [csv1;csv2]; % Concatenate vertically means 1-600
% 1-200
% 800
% Rows
writematrix(allCsv, strcat(strrep(data(dat).name, ' ', '_'), '')); % New File is created with 800 rows
end
end
end
end
1 个评论
Stephen23
2022-7-3
As an aside, note that these lines of code:
sensors = dir(home_dir);
for sens =3:length(sensors)
are a buggy attempt to deal with the dot directory names. Robust code uses SETDIFF or ISMEMBER.
As Walter Roberson wrote in that last link: "In short: if your code assumes that '.' and '..' are the first two entries in a directory, your code has a bug (even in MS Windows). If your code assumes that directory entries are returned in any sorted order, your code has a bug (in all OS.)"
"when ever i check the data in cell i observe that values are truncated"
Nothing in your code seems to "truncate" values. Please upload two sample files by clicking the paperclip button.
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 File Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!