splitting Excel file into 2 files
1 view (last 30 days)
Show older comments
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 Comments
Answers (1)
Walter Roberson
on 8 Jun 2018
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 Comments
Walter Roberson
on 8 Jun 2018
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)
See Also
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!