splitting Excel file into 2 files

1 visualizzazione (ultimi 30 giorni)
Roozbeh Yousefnejad
Roozbeh Yousefnejad il 8 Giu 2018
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)

Risposte (1)

Walter Roberson
Walter Roberson il 8 Giu 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 Commenti
Walter Roberson
Walter Roberson il 8 Giu 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)
Roozbeh Yousefnejad
Roozbeh Yousefnejad il 8 Giu 2018
Sorry, my bad, I think I made confusion. my csv file originally has 36 columns; however, to simplify here = I said 5 columns. to prevent confusion I attached the complete excel file. I am interested to check the number in the last columns which is column 36. I tried to change your script from 5 to 36 and definately this column is not empty and has some number more than 2300, but still get the same error.

Accedi per commentare.

Tag

Prodotti


Release

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by