Find unique rows in cell array with mixed data types

조회 수: 6 (최근 30일)
prp0rter
prp0rter 2016년 5월 9일
댓글: prp0rter 2016년 5월 13일
First I'm used to working in database structures where you have a table with a primary key linked to a table with foreign key. In matlab, I need to read an xls file where all the data is in one file. For example,
raw =
ID Type Customer Allergies SomeNumber Topping
1 pizza John NaN 9 Pepperoni
1 pizza John NaN 9 Mushrooms
1 pizza John NaN 9 Peppers
2 pizza Jill NaN NaN Olives
2 pizza Jill NaN NaN Canadian Bacon
I would then like to put this in a struct where the toppings are an array and the unique row values are represented one time. I tried using unique() but couldn't perform on a mixed cell array. Any ideas how to extract the unique information and then attach the non-unique elements? I ultimately want to construct a class with each entry so that I can loop through and output data to a different format.

채택된 답변

Ahmet Cecen
Ahmet Cecen 2016년 5월 9일
편집: Ahmet Cecen 2016년 5월 9일
I cannot recreate your problem. Here is what I did:
AA = {'A','B','A','C'};
BB = [1 2 1 1];
CC= [true,true,true,false];
Z=table(AA',BB',CC')
Var1 Var2 Var3
____ ____ _____
'A' 1 true
'B' 2 true
'A' 1 true
'C' 1 false
then simply:
unique(Z)
ans =
AA BB CC
___ __ _____
'A' 1 true
'B' 2 true
'C' 1 false
Correctly gives unique rows. IT is also possible I didn't understand your problem.
  댓글 수: 4
prp0rter
prp0rter 2016년 5월 13일
Thank you for your time to comment. To clarify, I am trying to eliminate duplicates and find all the unique rows and the file I'm reading from has mixed values strings, values, empty cells. So when I eliminate the nans and convert cell2table, I still get the error message "Input A must be a cell array of strings". My first value is a number but I don't know what difference that makes but obviously, Unique() won't process both numeric and string values in either a table or cell array.
prp0rter
prp0rter 2016년 5월 13일
I converted all the double data types to char using the following:
charArr = cellfun(@num2str, raw, 'Un', 0 );
Then
unique (charArr, 'rows');
and get the correction 'rows' is not supported for cell array inputs.
So then I converted my cell array to Table
T = cell2Table(charArr);
unk = unique(T, 'rows');
and that worked I got
1 pizza John "" 9
2 pizza Jill "" ""
Now I can loop through the toppings using the ID number and put into a struct. So the issue was converting to all one data type then using a Table for finding unique rows.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by