join tables based on string in one cell in the other

2 vues (au cours des 30 derniers jours)
jkr
jkr le 9 Fév 2018
Commenté : jkr le 9 Fév 2018
I have 2 tables, call them foo and bar. foo has strings in the form "foobar" in column 1. bar has strings in the form 'foobar' in column one.
foo{1,1} = "060514191658"
bar{1,1} = 1×1 cell array {'060514191658'}
foo(1,1) = table CaseID "060514191658"
bar(1,1) = table CaseID '060514191658'
When I try
>> join(foo,bar)
I get:
Error using tabular/join (line 129)
Left and right key variables 'CaseID' and 'CaseID' are not comparable because one is a non-cell.
How do I convert one or another to be able to join on this column.
I've tried every conversion I can think of and still can't get there.
  1 commentaire
per isakson
per isakson le 9 Fév 2018
You have different types of strings as value of CaseID. See

Connectez-vous pour commenter.

Réponse acceptée

Eric Tao
Eric Tao le 9 Fév 2018
You need to convert strings in foo.var1 to characters, assuming the 1st column name in foo and bar are both 'var1'.
Run:
foo.var1_converted = cellfun(@(x) char(x),foo.var1,'UniformOutput',false);
then you will get a new column 'var1_converted' in foo, which contains the converted characters from strings in column 'var1'.
Then run:
new_tab = join(foo,bar,'leftkeys','var1_converted','rightkeys','var1');
you will get your joined table.
  1 commentaire
jkr
jkr le 9 Fév 2018
Excellent, thank you, just what I needed.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Data Type Conversion dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by