How to check plot-properties for multiple lines in one plot - Grader

18 views (last 30 days)
Hello everyone,
iam trying to write a grader-assessment that's comparing plot-properties.
The students have to use given colors for 3 different plots in the same plot. But currently iam struggeling to check those properties..
I had the idea to save the information of the gca in a variable in the students template.
hold on
plot(diode3d(:,1,1),diode3d(:,2,1),'r');
plot(diode3d(:,1,2),diode3d(:,2,2),'b');
plot(diode3d(:,1,3),diode3d(:,2,3),'g');
hold off
ax = get(gca);
The line-options are being saved in 'Children' as line-array, so i though it should be possible to compare the line-color for all 3 lines with a for-loop. And in case the color doesn't fit for any of the line-childrens, an error with feedback appears. Even one wrong color would be enough to give the same feedback - doesn't matter which one is wrong
% Get reference solution for ax.
axReference = referenceVariables.ax;
for i=1:3
assert(ax.Children(i).Color == axReference.Children(i).Color);
end
But iam only getting the error-message "Invalid or deleted object."... Does anyone have an idea, what iam missing?
Thanks you very much in advance! :)
  1 Comment
Mitch Lautigar
Mitch Lautigar on 21 Apr 2022
When comparing plot properties, there are several things to keep in mind. Predominantly, the figure has to be open so it can be treated as a valid object. Once it's open, it's simply a matter of setting the figure title to a variable to check the properties. See answers below for if creating a figure, or opening a figure. I've also posted a link on accessing figure properties and what they are down at the bottom of my post.
If opening a figure (assuming this is done from a file that was was saved to the workspace):
f = figure(n); where n is whatever the figure number is. If the figure has a title, n would need to equal the title.
If creating a figure,
f = figure; %still creates a new figure, but leaves the properties in f where they can be edited and controlled.
Hope this helps!

Sign in to comment.

Accepted Answer

Cris LaPierre
Cris LaPierre on 21 Apr 2022
Edited: Cris LaPierre on 21 Apr 2022
Any figures created by the reference solution are closed and their objects deleted before the learner solution is executed. The simplest way to check, then, is to just hard code the reference color into your test.
You cannot make any assumptions about the learners plot, meaning that in order to grade the line properties you must
  1. check that there is one figure
  2. check that that figure has one axes
  3. check that that axes contains 3 lines
  4. check the color property of each line
The MATLAB Code assessment for doing that might look something like this. In order for this approach to work, the learner solution must plot the lines in the expected order. Be sure to tell your learners that order in the problem description.
% There should be one figure
figs = findobj('Type', 'figure');
assert(length(figs)==1, 'More than one figure found. Your code should only create a single figure');
% There should be 1 axes on the figure
ax = findobj(figs,'type','axes');
assert(length(ax)==1, 'More than one axes found. Your code should only create a single axes.');
% There should be 3 lines on the axes
line = ax.Children;
assert(length(line)==3, 'Your plot does not have the correct number of lines');
% Check the color property of each line.
% NOTE: the order is opposite what you might have expected: line(3) was plotted first
% and line(1) was plotted last
assert(all(line(3).Color == [1 0 0]), 'The color of the first line plotted is incorrect') % 'r'
assert(all(line(2).Color == [0 0 1]), 'The color of the second line plotted is incorrect') % 'b'
assert(all(line(1).Color == [0 1 0]), 'The color of the third line plotted is incorrect') % 'g'
  2 Comments
Cris LaPierre
Cris LaPierre on 21 Apr 2022
Since this does not use any of the assessment functions unique to MATLAB Grader, you can test the assessment code outside of Grader (here, for example). If no error appears, the generated figure has passed the assessment tests.
% Create a sample data set
diode3d = rand(5,2,3);
% plot the data
hold on
plot(diode3d(:,1,1),diode3d(:,2,1),'r');
plot(diode3d(:,1,2),diode3d(:,2,2),'b');
plot(diode3d(:,1,3),diode3d(:,2,3),'g');
hold off
% Run assessment code
% There should be one figure
figs = findobj('Type', 'figure');
assert(length(figs)==1, 'More than one figure found. Your code should only create a single figure');
% There should be 1 axes on the figure
ax = findobj(figs,'type','axes');
assert(length(ax)==1, 'More than one axes found. Your code should only create a single axes.');
% There should be 3 lines on the axes
line = ax.Children;
assert(length(line)==3, 'Your plot does not have the correct number of lines');
% Check the color property of each line
assert(all(line(3).Color == [1 0 0]), 'The color of the first line plotted is incorrect') % 'r'
assert(all(line(2).Color == [0 0 1]), 'The color of the second line plotted is incorrect') % 'b'
assert(all(line(1).Color == [0 1 0]), 'The color of the third line plotted is incorrect') % 'g'
Markus Großmann
Markus Großmann on 23 Apr 2022
The information in which order the reference and learner solution are checked & closed was the key.
Thank you very much!

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!