Dimensions of arrays being concatenated are not consistent error.

3 views (last 30 days)
How to fix this error?
Error using HelperTestKNNClassifier (line 35)
Could not concatenate the table variable 'ActualSpeaker' using VERTCAT.
Caused by:
Error using vertcat
Dimensions of arrays being concatenated are not consistent.
for idx = 1:length(Filenames)
T = featuresTest(Fidx==idx,2:end); % Rows that correspond to one file
predictedLabels = string(predict(trainedClassifier,T(:,1:15))); % Predict
totalVals = size(predictedLabels,1);
[predictedLabel, freq] = mode(categorical(predictedLabels)); % Find most frequently predicted label
match = freq/totalVals*100;
result_file.Filename = Filenames(idx);
result_file.ActualSpeaker = T.Label{1};
result_file.PredictedSpeaker = char(predictedLabel);
result_file.ConfidencePercentage = match;
result = [result; struct2table(result_file)]; %#ok
end

Answers (1)

dpb
dpb on 15 Dec 2018
Edited: dpb on 15 Dec 2018
result_file.ActualSpeaker = T.Label{1};
...
result = [result; struct2table(result_file)];
You're trying to catenate char() arrays that may not be the same length (and, in fact, aren't). This happened because you first dereference a cell string to char, then tried to build a table. Illustration of barebones problem:
>> T.Label='Label 1'; % first char label (did the {} dereference manually)
>> res=struct2table(T); % first entry into a table is ok...
>> T.Label='Label 1001'; % later on the string is longer...
>> res=[res;struct2table(T)] % and, BOOM!
Could not concatenate the table variable 'Label' using VERTCAT.
Caused by:
Error using vertcat
Dimensions of matrices being concatenated are not consistent.
>>
The solution is to not dereference the label but leave as cell strings (or, if appropriate, convert to categorical )
result_file.ActualSpeaker = T.Label(1);

Categories

Find more on Numeric Types in Help Center and File Exchange

Tags

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!