Why do I obtain an error about string cells when I try to join two datasets in MATLAB (R2013a)?
1 view (last 30 days)
Show older comments
MathWorks Support Team
on 25 Oct 2013
Answered: MathWorks Support Team
on 25 Oct 2013
I am using the function JOIN on two datasets:
>> C = join(dataset1,dataset2);
but I obtain the following error:
Error using dataset/join (line 274)
Left and right key variables 'key1' and 'key1' include cells containing non-string values.
Accepted Answer
MathWorks Support Team
on 25 Oct 2013
The reason behind the error is that the content of your dataset, under the variable name 'key1' must be of type 'char", and not "double", as in your case.
To circumvent this issue, you need to do a data-type conversion. Here is an example that illustrates that. Say, your variable name tagged "key1" is located in column "idx1" in "dataset1" and in column "idx2" in "dataset2".
>> varName1 = dataset1.Properties.VarNames(idx1);
>> cellData1 = dataset2cell(dataset1(:,idx1));
>> data1 = cellData1(2:end,2);
>> data1 = cellfun(@num2str,data1,'UniformOutput',false);
>> ds1 = dataset(data1,'Varnames',varName1,'ObsNames',dataset1.Properties.ObsNames);
>> dataset1(:,idx1) = ds1;
>> varName2 = StatSet.Properties.VarNames(idx2);
>> cellData2 = dataset2cell(dataset2(:,idx2));
>> data2 = cellData2(2:end,2);
>> data2 = cellfun(@num2str,data2,'UniformOutput',false);
>> ds2 = dataset(data2,'Varnames',varName2,'ObsNames',dataset2.Properties.ObsNames);
dataset2(:,idx2) = ds2;
C = join(dataset1,dataset2,'Key','key1');
0 Comments
More Answers (0)
See Also
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!