Plot multiple separate figures

4 views (last 30 days)
BN
BN on 12 Apr 2020
Commented: Ameer Hamza on 12 Apr 2020
Dear all,
I have a table with two rows, I have a code to plot a pie chart for a first row, Now for each row I want to have a separate figure. In fact, I want to have a loop to generate a pie chart for each row and save it as a figure in the current folder. I tried some ways but all of them gives me various errors.
Here is the code:
clf
p = pie(ones(1,5));
t = p(2:2:end);
p = p(1:2:end);
delete(t)
s = {'CC', 'ME', 'NU', 'BI', 'IA'};
for i=1:5
switch davar1.([s{i} '_CHECK']) % I even tried insert (j) here and do it in the for loop but error says Index exceeds the number of array elements (2).
case 'New York'
p(i).FaceColor = 'g';
case 'California'
p(i).FaceColor = 'y';
case 'Illinois'
p(i).FaceColor = 'r';
case 'Texas '
p(i).FaceColor = 'k';
case 'Ohio'
p(i).FaceColor = 'b';
case 'North Carolina'
p(i).FaceColor = 'c';
case 'Tennessee'
p(i).FaceColor = 'w';
end
end
I attached the table, this is a sample data because the original file is too large to attach and have more than 90 rows.
Really Thank you

Accepted Answer

Ameer Hamza
Ameer Hamza on 12 Apr 2020
Try this
clf
f = figure();
ax = axes();
p = pie(ax, ones(1,5));
t = p(2:2:end);
p = p(1:2:end);
delete(t)
s = {'CC', 'ME', 'NU', 'BI', 'IA'};
for k=1:size(davar1, 1)
for i=1:numel(s)
switch davar1(k,:).([s{i} '_CHECK']) % I even tried insert (j) here and do it in the for loop but error says Index exceeds the number of array elements (2).
case 'New York'
p(i).FaceColor = 'g';
case 'California'
p(i).FaceColor = 'y';
case 'Illinois'
p(i).FaceColor = 'r';
case 'Texas '
p(i).FaceColor = 'k';
case 'Ohio'
p(i).FaceColor = 'b';
case 'North Carolina'
p(i).FaceColor = 'c';
case 'Tennessee'
p(i).FaceColor = 'w';
end
end
exportgraphics(ax, ['filename' num2str(k) '.png']);
end
  2 Comments
BN
BN on 12 Apr 2020
This worked perfectly. Really Thank you. ??
Ameer Hamza
Ameer Hamza on 12 Apr 2020
Glad to be of help.

Sign in to comment.

More Answers (0)

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!