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
JESUS DAVID ARIZA ROYETH
JESUS DAVID ARIZA ROYETH 2019년 12월 5일
how you read your file to obtain MASSADOC?
Mikel Spillemaekers
Mikel Spillemaekers 2019년 12월 5일
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
Mikel Spillemaekers 2019년 12월 6일
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개)

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by