Why does categories(TableA.Var2) have more elements than the rows in TableA?
Show older comments
I do not understand the behaviour I'm seeing when I use categories(TableA.Var2) with a table. I'm getting more categories than the amount of rows in the table and I'm also seeing values in the categories list that do not appear in the column of the table that I'm looking at.
I read a table from an Excel file and then want to create a list of the unique Fault Labels. When I use unique(ModuleFC.FaultLabel) I get a list of the unique Fault Labels. I'm very confused why my attempt to use FaultList = categories(ModuleFC.FaultLabel) gives me more elements than the amount of rows in the table.
function FaultCodeGroups
%% Import data from spreadsheet
% Script for importing data from the following spreadsheet:
%
% Workbook: C:\FaultCodesTable.xlsx
% Worksheet: FaultCodesTable
%
% To extend the code for use with different selected data or a different
% spreadsheet, generate a function instead of a script.
% Auto-generated by MATLAB on 2020/01/16 12:14:02
%% Import the data
[~, ~, raw] = xlsread('C:\FaultCodes.xlsx','FaultCodes');
raw(cellfun(@(x) ~isempty(x) && isnumeric(x) && isnan(x),raw)) = {''};
stringVectors = string(raw(:,[1,2,3]));
stringVectors(ismissing(stringVectors)) = '';
%% Replace non-numeric cells with NaN
R = cellfun(@(x) ~isnumeric(x) && ~islogical(x),raw); % Find non-numeric cells
raw(R) = {NaN}; % Replace non-numeric cells
%% Create table
FaultCodesTable = table;
%% Allocate imported array to column variable names
FaultCodesTable.Model = categorical(stringVectors(:,1));
FaultCodesTable.FaultLabel = categorical(stringVectors(:,2));
FaultCodesTable.Module = categorical(stringVectors(:,3));
%% Clear temporary variables
clearvars data raw stringVectors R;
% Get a list of all the different car models
CarList = categories(FaultCodesTable.Model);
% Get a list of all the modules
ModuleList = categories(FaultCodesTable.Module);
%% Extract the fault codes for each car line
for CLCount = 1:numel(CarList)
% Extract table with only the fault codes of the currently selected car line
CarLineFaultCodes = FaultCodesTable(FaultCodesTable.Model == CarList{CLCount},:);
% Delete FaultCodesTable to ensure its Faultlabel column does not exist in memory
% This is only for debugging. Can not use this in actual for loop
FaultCodesTable = [];
%% Extract the fault codes for each module
for MCount = 1:numel(ModuleList)
% Extract the fault codes for the currently selected module
ModuleFC = CarLineFaultCodes(CarLineFaultCodes.Module == ModuleList{MCount},:);
% Delete CarLineFaultCodes to ensure its Faultlabel column does not exist
% This is only for debugging. Can not use this in actual for loop
CarLineFaultCodes = [];
% ModuleFC has 996 rows
% Get a list of the unique Fault Labels in ModuleFC
UniqueFaults = unique(ModuleFC.FaultLabel);
numel(UniqueFaults) % 59 unique Fault Labels
% Get categories in FaultLabel column of ModuleFC
FaultList = categories(ModuleFC.FaultLabel);
UniqueFL = unique(FaultList);
numel(UniqueFL) % 1127 ,but ModuleFC has 996 rows ???
end
end
end
Accepted Answer
More Answers (0)
Categories
Find more on Large Files and Big Data 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!