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

7 views (last 30 days)
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?

Accepted Answer

dpb
dpb on 25 Oct 2022
Edited: dpb on 26 Oct 2022
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 Comments
dpb
dpb on 26 Oct 2022
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!