Size in byte of file reduced when i combine two csv

1 view (last 30 days)
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 Comment
Stephen23
Stephen23 on 3 Jul 2022
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.

Sign in to comment.

Answers (0)

Categories

Find more on File Operations in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!