Is it possible to summarize this code?
2 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
Jim Riggs
2020 年 4 月 11 日
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 件のコメント
その他の回答 (1 件)
Walter Roberson
2020 年 4 月 12 日
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.
参考
カテゴリ
Help Center および File Exchange で Data Distribution Plots についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!