MATLAB Answers

# Is it possible to summarize this code?

1 view (last 30 days)
Behzad Navidi on 11 Apr 2020
Commented: Behzad Navidi on 13 Apr 2020
Hey all,
Is it possible to summarize this code below?
This code creates a pie chart with 5 equal parts, then checks for city name (seven cities in total) that wrote in a first to five columns of a table and set the color of the patches based on the name.
Actually the color of the first to the fifth patch of pie chart determines based on the name of the city in the first to the fifth column of the table.
clf
p = pie(ones(1,5));
t = p(2:2:end);
p = p(1:2:end);
delete(t)
%______________________________________________________________________________________________
% For the first patch (check with first column of table namely: CC_CHECK
%______________________________________________________________________________________________
if davar1.CC_CHECK=='New York'
p(1).FaceColor = 'g';
end
if davar1.CC_CHECK=='California'
p(1).FaceColor = 'y';
end
if davar1.CC_CHECK=='Illinois'
p(1).FaceColor = 'r';
end
if davar1.CC_CHECK=='Texas '
p(1).FaceColor = 'k';
end
if davar1.CC_CHECK=='Ohio'
p(1).FaceColor = 'b'; % powder pink
end
if davar1.CC_CHECK=='North Carolina'
p(1).FaceColor = 'c';
end
if davar1.CC_CHECK=='Tennessee'
p(1).FaceColor = 'w';
end
%______________________________________________________________________________________________
% Again this part but for the second patch (CHECKES SECOND COLUMN OF THE TABLE Namely ME_CHECK)
%______________________________________________________________________________________________
if davar1.ME_CHECK=='New York'
p(2).FaceColor = 'g';
end
if davar1.ME_CHECK=='California'
p(2).FaceColor = 'y';
end
if davar1.ME_CHECK=='Illinois'
p(2).FaceColor = 'r';
end
if davar1.ME_CHECK=='Texas '
p(2).FaceColor = 'k';
end
if davar1.ME_CHECK=='Ohio'
p(2).FaceColor = 'b'; % powder pink
end
if davar1.ME_CHECK=='North Carolina'
p(2).FaceColor = 'c';
end
if davar1.ME_CHECK=='Tennessee'
p(2).FaceColor = 'w';
end
%______________________________________________________________________________________________
% For the Third Patch (useing the third column namely: NU_CHECK)
%______________________________________________________________________________________________
if davar1.NU_CHECK=='New York'
p(3).FaceColor = 'g';
end
if davar1.NU_CHECK=='California'
p(3).FaceColor = 'y';
end
if davar1.NU_CHECK=='Illinois'
p(3).FaceColor = 'r';
end
if davar1.NU_CHECK=='Texas '
p(3).FaceColor = 'k';
end
if davar1.NU_CHECK=='Ohio'
p(3).FaceColor = 'b'; % powder pink
end
if davar1.NU_CHECK=='North Carolina'
p(3).FaceColor = 'c';
end
if davar1.NU_CHECK=='Tennessee'
p(3).FaceColor = 'w';
end
%______________________________________________________________________________________________
% For the fourth Patch (check with the fourth column of the table namely BI_CHECK)
%______________________________________________________________________________________________
if davar1.BI_CHECK=='New York'
p(4).FaceColor = 'g';
end
if davar1.BI_CHECK=='California'
p(4).FaceColor = 'y';
end
if davar1.BI_CHECK=='Illinois'
p(4).FaceColor = 'r';
end
if davar1.BI_CHECK=='Texas '
p(4).FaceColor = 'k';
end
if davar1.BI_CHECK=='Ohio'
p(4).FaceColor = 'b'; % powder pink
end
if davar1.BI_CHECK=='North Carolina'
p(4).FaceColor = 'c';
end
if davar1.BI_CHECK=='Tennessee'
p(4).FaceColor = 'w';
end
%______________________________________________________________________________________________
% For the fifth Patch (last) (check with the fifth column of the table namely IA_CHECK)
%______________________________________________________________________________________________
if davar1.IA_CHECK=='New York'
p(5).FaceColor = 'g';
end
if davar1.IA_CHECK=='California'
p(5).FaceColor = 'y';
end
if davar1.IA_CHECK=='Illinois'
p(5).FaceColor = 'r';
end
if davar1.IA_CHECK=='Texas '
p(5).FaceColor = 'k';
end
if davar1.IA_CHECK=='Ohio'
p(5).FaceColor = 'b'; % powder pink
end
if davar1.IA_CHECK=='North Carolina'
p(5).FaceColor = 'c';
end
if davar1.IA_CHECK=='Tennessee'
p(5).FaceColor = 'w';
end
I attach the table too. I would be grateful If you can tell me it is possible to summarize this code or not?
Really thank you all
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Accepted Answer

Jim Riggs on 11 Apr 2020
You could use a switch case structure, that would be a bit more compact.
it would look like this:
switch davar1.CC_CHECK
case 'New York'
p(1).FaceColor = 'g';
case 'California'
p(1).FaceColor = 'y';
case 'Illinois'
p(1).FaceColor = 'r';
case 'Texas '
p(1).FaceColor = 'k';
case 'Ohio'
p(1).FaceColor = 'b'; % powder pink
case 'North Carolina'
p(1).FaceColor = 'c';
case 'Tennessee'
p(1).FaceColor = 'w';
end
##### 3 CommentsShowHide 2 older comments
Behzad Navidi on 13 Apr 2020
Thank you all, I accept this answer but Walter Roberson answer is perfect too. Thanks a lot

Sign in to comment.

### More Answers (1)

Walter Roberson on 12 Apr 2020
p(1).FaceColor = find_color(davar1.CC_CHECK);
p(2).FaceColor = find_color(davar1.ME_CHECK);
function col = find_color(location)
locations = {'New York', 'California', 'Illinois', 'Texas ', 'Ohio', 'North Carolina', 'Tennessee'};
colors = {'g', 'y', 'r', 'k', '#ffb2d0', 'c', 'w'};
col = '#4A412A'; %use ugliest color in the world if location not found
[found, idx] = ismember(location, locations);
if found; col = colors{idx}; end
end
Please verify, though that the Texas entry really does have spaces at the end of it. And decide which color you want for the case where the location does not match any of the predefined locations.
##### 1 CommentShowHide None
Behzad Navidi on 13 Apr 2020
Thank you so much. +1

Sign in to comment.

R2020a

### Community Treasure Hunt

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

Start Hunting!

Translated by