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 Center および File ExchangeData Type Conversion についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by