splitting Excel file into 2 files
1 次查看(过去 30 天)
显示 更早的评论
Hi, I am wondering how I can split an excel file into 2 new one based on the criteria. say my excel file has 5 columns and 10 rows. I want to check the 5th column and if the number in that column is more than 2300, I want to separate a row completely and put it in one spreadsheet and if it is less than 2300 separate it and put it in another spreadsheet.
can you please advise what I can do? or what is the proper function to do it? ( I have attached a sample file)
0 个评论
回答(1 个)
Walter Roberson
2018-6-8
T = readtable('result.csv', 'readvariable', false);
mask = T{:,5} > 2300;
T_high = T(mask,:);
writetable(T_high, 'NewTable.xlsx', 'Sheet', 'high', 'writevariable', false);
T_low = T(~mask, :);
writetable(T_low, 'NewTable.xlsx', 'Sheet', 'low', 'writevariable', false);
3 个评论
Walter Roberson
2018-6-8
That would only happen if column 35 was a cell array, such as column 35 was a cell array of character vectors.
... But you said that you have 5 columns... and your example data has 7 columns and the 5th column never exceeds 2300
I should revise what I posted slightly:
T = readtable('result.csv', 'readvariable', false);
mask = T{:,5} > 2300;
T_high = T(mask,:);
if ~isempty(T_high)
writetable(T_high, 'NewTable.xlsx', 'Sheet', 'high', 'writevariable', false);
end
T_low = T(~mask, :);
if ~isempty(T_low)
writetable(T_low, 'NewTable.xlsx', 'Sheet', 'low', 'writevariable', false);
end
as it turns out that writetable errors out if you ask to write an empty table (empty because nothing in column 5 of your sample data exceeds the threshold)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!