Hi all, I have one csv file and I am planning to split it into two separate sheet based on a criteria. Th first column of my CSV has "name of the file" and all other columns have numbers. For each row, I want to check the last column and if it is more than 2300, separate the row and put it in one sheet of CSV, else put it in another sheet of CSV. I came up with this code
jj=1
cc=1
for j=1 : 463
if result(j,35)<2380
LF(jj,:)=result(j,:);
Lname{jj,1}=result1{j,1};
jj=jj+1;
else
HF(cc,:)=result(j,:);
Hname{cc,1}=result1{j,1};
cc=cc+1
end
end
sheet1=1;
sheet2=2;
xlswrite('seperated',Lname,sheet1,'A1')
xlswrite('seperated',LF,sheet1,'B1')
xlswrite('seperated',Hname,sheet2,'A1')
xlswrite('seperated',HF,sheet2,'B1')
It works, however, I am not sure when I want to write it in csv file, why I cannot not write the Lname which is a cell and include the related name can you please advise what I need to do to write it?

10 Comments

Can you add your input .csv file?
sure, it is added, Thanks Paolo
Paolo, Please consider that
variable "result" includes all the columns which have numbers variable "result1" includes only a first column which has name
@roozbeh
t = readtable('result.csv');
c = table2cell(t);
r = cell2mat(c(:,36))>2300;
datagreater = c(r,:);
datalower = c(~r,:);
xlswrite('datagreater',datagreater);
xlswrite('datalower',datalower);
Does this code do what you want?
unfortunately it doesn't work. This code is smarter than mine, but it gives me the same result. The main problem is that at the end in the generated excel file I do not have the name of the files and the first column is empty
Please advise how I can add the first column which includes the names
@Paolo please see the attached file, as you can see the first column is empty. I want to have also the name of the file in the first column
The main problem is that at the end in the generated excel file I do not have the name of the files and the first column is empty
I am unsure as to why the first column is empty for you. What version of Matlab are you using? And what do you mean by:
I do not have the name of the files
Do you want to repeat the first column or have a new, final column with the name of the file, e.g. 'datagreater' and 'datalower' repeated?
Please find attached 'datagreater.xls' and 'datalower.xls' which I obtained by running the code I shared in the previous comment.
I checked the attached file and it is okay now and this is exactly what I want. However, the problem is when I run your script and even mine the first column of the xls files which is for the name of the files are empty for me. I do not understand why:-| I am using MATLAB 2017 a. Probably it has something to do with version of MATLAB.
I appreciate you time and really thanful for your help
t = readtable('result.csv');
c = table2cell(t);
r = cell2mat(c(:,36))>2300;
That's a lot of unnecessary conversions here
t = readtable('result.csv');
r = t{:, 36} > 2300;
would achieve the same faster. There is no point in converting the table to anything else. You can just split the table itself.
That's a very good point.

Sign in to comment.

 Accepted Answer

t = readtable('result - Copy.csv');
flag = t{:, 36} > 2300;
writetable(t(flag, :), 'datagreater.xls', 'WriteVariableNames', false);
writetable(t(~flag, :), 'datalower.xls', 'WriteVariableNames', false);
Should be all that is needed.

More Answers (0)

Products

Release

R2017b

Community Treasure Hunt

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

Start Hunting!