How to split the table selectively and create another table by giving new column names?

Hello. In a csv table, two types of data are mixed. For the first data, there are 25 columns with multiple rows and at the end of the first data, there is second data with three columns. I don't know how to split into two tables.
After loading the table to matlab, I don't know how to split the table. The heading of the second set of data is also disappeared.
I attached the ''csv'' file here. Can someone help me, pls?

 Risposta accettata

Well, I had a full, long executing example that ran but then the forum page crashed -- I'm too lazy to try to redo that whole thing again so will just post the code...
The file does contain same number of delimiters in all records and is well-formed in that way...just find the header locations of interest and use those to read the two sections independently.
fn='trial_excel.csv';
l=readlines(fn); % suck it all in as string array to find locations
l=l(strlength(l)>0); % there is an empty record
ix=find(startsWith(l,'Date')); % the header records -- the two date ranges are there is well; didn't parse them here
% clear l % if don't care about other records to parse besides can save memory
rnge=sprintf('%d:%d',ix(2),ix(3)-1); % first header to just before second time range record
tT1=readtable(fn,'range',rnge,'delimiter',';','decimalseparator',',','VariableNamingRule','preserve');
rnge=ix(4); % second header; just use start row to end
tT2=readtable(fn,'range',rnge,'delimiter',';','decimalseparator',',','VariableNamingRule','preserve');

4 Commenti

Thank you very much for helping me. When I run the code, it only creates tT2 table (the data with 3 columns) and tT1 table (the data with 25 columns) is empty somehow. I am trying to read the tT1 but I can't. Can you help me, please?
Thank you very much for helping me. When I run the code, it only creates tT2 table (the data with 3 columns) and tT1 table (the data with 25 columns) is empty somehow. I am trying to read the tT1 but I can't. Can you help me, please?
fn='trial_excel.csv';
l=readlines(fn); % suck it all in as string array to find locations
l=l(strlength(l)>0); % there is an empty record
ix=find(startsWith(l,'Date')); % the header records -- the two date ranges are there is well; didn't parse them here
rnge=[ix(2) ix(3)-1]; % first header to just before second time range record
tT1=readtable(fn,'range',rnge,'delimiter',';','decimalseparator',',','VariableNamingRule','preserve')
tT1 = 0×0 empty table
rnge=[ix(4)]; % second header; just use start row to end
tT2=readtable(fn,'range',rnge,'delimiter',';','decimalseparator',',','VariableNamingRule','preserve')
tT2 = 270×1 table
Date→Time_fre→'F_(10s)_[Hz]' __________________________________ {'01.01.1970→00:00:00.000' } {'02.09.2021→09:02:40.000→49,955'} {'02.09.2021→09:02:50.000→49,956'} {'02.09.2021→09:03:00.000→49,955'} {'02.09.2021→09:03:10.000→49,962'} {'02.09.2021→09:03:20.000→49,96' } {'02.09.2021→09:03:30.000→49,961'} {'02.09.2021→09:03:40.000→49,969'} {'02.09.2021→09:03:50.000→49,966'} {'02.09.2021→09:04:00.000→49,967'} {'02.09.2021→09:04:10.000→49,982'} {'02.09.2021→09:04:20.000→49,986'} {'02.09.2021→09:04:30.000→49,982'} {'02.09.2021→09:04:40.000→49,983'} {'02.09.2021→09:04:50.000→49,974'} {'02.09.2021→09:05:00.000→49,974'}
I cut a corner on the repost thinking could pass the 'range' rows as a vector -- turns out that's not so, it has to be an Excel-like range string to have the start:end rows, not just the starting row that can be just a numeric value.
I had done it in the original then got mad/frustrated after had spent the time/effort and the page crashed on me and tried to slough it off...sorry.
Use
rnge=sprintf('%d:%d',ix(2),ix(3)-1);
instead and joy shall ensue...it's unfortunate there's no warning/error for the other form; I'll have to think about that some as to whether that's worthy of a bug/enhancement report or not.
Thank you very much. It works now.

Accedi per commentare.

Più risposte (0)

Prodotti

Richiesto:

il 25 Ott 2022

Modificato:

dpb
il 26 Ott 2022

Community Treasure Hunt

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

Start Hunting!

Translated by