Dimensions of arrays being concatenated are not consistent error.

3 次查看(过去 30 天)
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

回答(1 个)

dpb
dpb 2018-12-15
编辑:dpb 2018-12-15
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);

类别

Help CenterFile Exchange 中查找有关 Numeric Types 的更多信息

标签

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by