How to change value in a table so the old one is no longer in the table

2 views (last 30 days)
Hi everyone, I am trying to replace values in the table pizzasize using code
ind = find(pizzasize.CrustDescription == 'MidCrust');
for i = 1 : size(ind)
pizzasize.CrustDescription(ind(i)) = 'Mid';
end
ind = find(pizzasize.CrustDescription == 'ClassicCrust');
for i = 1 : size(ind)
pizzasize.CrustDescription(ind(i)) = 'Mid';
end
ind = find(pizzasize.CrustDescription == 'ThinNCrispy');
for i = 1 : size(ind)
pizzasize.CrustDescription(ind(i)) = 'Thin';
end
ind = find(pizzasize.CrustDescription == 'ThinCrust');
for i = 1 : size(ind)
pizzasize.CrustDescription(ind(i)) = 'Thin';
end
but when I call summary(pizzasize) I get
CrustDescription: 250×1 categorical
Values:
ClassicCrust 0
DeepPan 83
MidCrust 0
ThinCrust 0
ThinNCrispy 0
Mid 85
Thin 82
How can I get rid of the values, that are no longer present in the table? Thanks for help.
  9 Comments
Paolo
Paolo on 2 Jun 2018
@Antonie You are welcome. I have submitted an answer to the question, please accept it so it will be easily visible to other users having the same question, and for the question to be market as closed.
Peter Perkins
Peter Perkins on 4 Jun 2018
Antonie, if I am reading your code correctly, you are combining two categories into one, and renaming the combined category. You want mergecats. It's a one-liner:
pizzasize.CrustDescription = mergecats(pizzasize.CrustDescription,{'MidCrust' 'ClassicCrust'},'Mid')
Also: all those loops were not really necessary. If mergecats didn't exist, you'd want to do this:
pizzasize.CrustDescription(pizzasize.CrustDescription == 'MidCrust') = 'Mid'
without a loop (and without a find, for that matter).

Sign in to comment.

Accepted Answer

Paolo
Paolo on 2 Jun 2018
You can remove categories from categorical arrays using removecats.
View your categories with command:
categories(pizzasize.CrustDescription)
The categories:
{'ClassicCrust'}
{'DeepPan' }
{'MidCrust' }
{'ThinCrust' }
{'ThinNCrispy' }
Remove 'DeepPan' category with the command:
pizzasize.CrustDescription = removecats(pizzasize.CrustDescription,'DeepPan');
The remaining categories:
categories(pizzasize.CrustDescription)
{'ClassicCrust'}
{'MidCrust' }
{'ThinCrust' }
{'ThinNCrispy' }

More Answers (0)

Categories

Find more on Categorical Arrays in Help Center and File Exchange

Tags

Products


Release

R2017a

Community Treasure Hunt

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

Start Hunting!