How to split the table selectively and create another table by giving new column names?
3 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
采纳的回答
dpb
2022-10-25
编辑:dpb
2022-10-26
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 个评论
dpb
2022-10-26
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!