Read and write csv files without changing properties

I've succesfully solved a big problem thanks to you guys and now I'm facing a slightly smaller but important one.
(Following code is the original one posted by another mvp here. Kudos)
data = int64(csvread('Sample_02.csv'));
time = data(:,2);
% remove empty rows
data = data(time>0, :);
time = time(time>0);
time = mod(time, 1000000); % yyyymmdd are not important so discard them
current_time = 00000; % time in hhmmss format
while size(data, 1) > 0
next_time = current_time + 30000; % 30000 represent 3 hours
index = time < (current_time + 30000);
partial_data = data(index, :);
dlmwrite(['data-' num2str(current_time/10000) '-' num2str(next_time/10000) '.csv'], ...
partial_data, 'precision', '%i');
data(index,:) = [];
time(index,:) = [];
current_time = current_time + 30000;
end
The code reads the sample CSV file and split it in to seperate csv files based on time-stamps.
My problem is: The original csv file (Sample_02.csv) has 7 columns and the above code only deals with the second one. Output files also have 7 columns, obviously, but the values are not shown/stored as decimals. For example, the original CSV file stores 123.456789 in its 6th column but the generated output files have only 123 in the respective 6th column.
How can I store the same data as decimals in the output files?
TIA!

댓글 수: 6

Don't use 'precision', '%i' -- the %i means to only write out integers.
Preserving the same number of decimal places as the original requires processing the lines as text rather than numeric.
Thank you!
But I did try removing 'precision', '%i' from the code but it only affects the first two columns of the output csv file. Also, output comes as a power (ex: 2.02002E+13 ) and not as the data stored in the original file. (Sample_02.csv)
Preserving the same number of decimal places as the original requires processing the lines as text rather than numeric.
So I should go with something other than csvread?
I'm sorry about the noob questions, I'm still quite new.
I tested your sample file with readtable and writetable. It seems to work as you desire. I am using R2019b.
You mean the output data were in the format with decimals? I'm using R2017a and it didn't work for some reason :(

댓글을 달려면 로그인하십시오.

 채택된 답변

Ameer Hamza
Ameer Hamza 2020년 3월 11일
편집: Ameer Hamza 2020년 3월 11일
Hi James, I posted the answer to your other question. You can just replace the line
data = int64(csvread('Sample_02.csv'));
to
data = csvread('Sample_02.csv');
and it will retain the decimal part of the numbers.
I initially used int64 to preserve precision in the 2nd column, but since it has 14 digits, so it can be handled without int64 too.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2020년 3월 10일

0 개 추천

fileread to read the file into a string.
textscan the string to extract the datetime as numeric.
Now regexp the string with '\r?\n', 'split' to create a cell array in which each entry is a line.
Use the information in the numeric date to figure out where to split. The indices can be used in the cell array of split lines. strjoin() those cells with '\n' and fwrite() the result.

댓글 수: 3

Thank you, Walter.
I'm still new so it's a bit complicated for me after the second line of suggestion. But I will try to work around.
S = fileread(filename);
fmt = '%*f,%ld,%*s';
C2 = cell2mat(textscan(S, fmt));
lines = regexp(S, '\r?\n', 'split');
time = double(mod(C2, 1e6));
...
mask = start_time <= time & time < end_time;
subset = strjoin(lines(mask), '\n') ;
fwrite(fid, subset);
Thank you!

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Data Type Conversion에 대해 자세히 알아보기

태그

질문:

2020년 3월 10일

댓글:

2020년 3월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by