How do I isolate rows from a table, containing the same column value?

5 次查看(过去 30 天)
I received a very large spreadsheet document with data of students, per student the columns exist of name, school, date of birth etc. I imported this spreadsheat as a table in matlab, and set all columns as strings.
The goal is to extract rows of students who went to the same highschool. Let's say this value is in collumn 3 and is alphabetically ordered.
I tried this in matlab by iterating and trying to save the students in a new table when the schools match. I found a solution where I have to copy one school in my code and then it gives a clean table with all students who went to that school.
But since there are 3000 different schools in the document it would be nice let a loop iterate over all schools and do all the work in one time.
This is what I thought would have worked but it didnt't:
MASSADOC = sortrows(MASSADOC,'school','ascend');
T=table;
Names=strings;
for i =1:15310%there are 15310 rows in my document
Names(i)=MASSADOC.Omschrijvingin(i); %to get a string array with all schools
end
U=unique(Names); %a string array with all schools 1 time in it
for i = 1:size(U)
x=U(i);
for j = 1:15310
if MASSADOC.school(j)==U(i)
T(j,:)=MASSADOC(j,:);
else
end
writetable(T, x+'.xlsx','Sheet',1);
end
end
  3 个评论
Mikel Spillemaekers
Massadoc2 is just a part of a copy of MASSADOC. So in the code MASSADOC should be Massadoc2 in this case.

请先登录,再进行评论。

采纳的回答

Guillaume
Guillaume 2019-12-5
编辑:Guillaume 2019-12-5
There is no need to sort the table beforehand:
[group, schoolname] = findgroups(MASSADOC.school); %get unique schools and assign unique group to each one
for g = 1:numel(schoolname) %iterate over each school/group
writetable(MASSADOC(group == g, :), sprintf('%s.xlsx', schoolname{g})); %and save the rows that match the group
end
  6 个评论
Mikel Spillemaekers
This works like a charm! Thank you very much. I think I'm too much thinking in a Python or Java way with the usage of If and For.
Do you have any idea how big excel files can go that matlab can handle? or does this just depend on you computer power?
Guillaume
Guillaume 2019-12-6
Ultimately, it all depends on your computer but most likely, Excel will struggle on a big excel sheet before matlab does since excel needs to keep in memory not only the cell values, but their formatting, formulae and other properties which matlab doesn't store.
In addition, in matlab you can always resort to datastore and tall arrays to deal with data that would never fit in memory.

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by