How can you validate plot title, xlabel, and ylabel on a MATLAB Grader Assessment?

29 views (last 30 days)
I am writing MATLAB Grader problems that make use of plots and/or figures. Can I use MATLAB Grader to evaluate whether the output of a plot is correct?

Accepted Answer

Jeff Alderson
Jeff Alderson on 7 Jul 2020
MATLAB Grader does not have a built-in way to check properties of a plot. This is partly because comparing strings is especially error prone (spaces, capitalization, spelling, extra info like units included or not, etc). The "Function or Keyword is Present" test only checks that the listed functions/keywords were used in the solution. It does not check their values or settings.
Our recommendation would be first evaluate the necessity of such assessment tests. If they are deemed necessary, we would recommend using the "Function or Keyword is Present" assessment test to check that the functions 'title', 'xlabel', and 'ylabel' were used in the learner solution rather than checking the actual values. You can see an example in the following sample problems:
  • Introduction to Programming > Graphing > Projectile trajectory (plot of multiple data series)
  • Introduction to Programming > Graphing > Plot a decaying cosine wave (basic plot of mathematical function)
  4 Comments
Justin Wyss-Gallifent
Justin Wyss-Gallifent on 1 Oct 2020
If it doesn't exist it might be worth writing a collection of code fragments which could help people with some experience but not total comfort. Such things can be found by digging through the various replies here but perhaps having someone cull the good ones?

Sign in to comment.

More Answers (2)

Keith Hekman
Keith Hekman on 4 Feb 2022
r=mg_isLabelInPlot('XLabel','t(s)','YLabel','x(m)')';
result = double(r);
assessVariableEqual('result',1)
%varargin (Char array1, value1, char array2, value2, ... , char arrrayN, valueN)
% possible requirements can be: 'XLabel','YLabel','ZLabel','Title'
%result (logical): 1 if lable is correct, 0 if not
function result = mg_isLabelInPlot(varargin)
result = false();
possibleProperties = ["XLabel","YLabel","ZLabel","Title"];
%% Check for empty input argumetns
if (isempty(varargin))
error("No arguments given.\nUsage: mg_isLabelInPlot('Property1', 'Wanted1', 'Property2', 'Wanted2', ...\nProperties can be:\n'XLabel'\n'YLabel'\n'ZLabel'\n'Title'\n", "");
end
%% Check if amount of inputs is a multiple of 2
if mod(length(varargin),2) ~= 0
error("Amount of input Arguments must be a multiple of 2!\n\nUsage: mg_isLabelInPlot('Property1', 'Wanted1', 'Property2', 'Wanted2', ...\nProperties can be:\n'XLabel'\n'YLabel'\n'ZLabel'\n'Title'\n", "");
end
%% Generate cell of searched for properties from varargin
if length(varargin) == 2
properties = varargin;
else
properties = reshape(varargin, [2, length(varargin)/2])';
end
%% Check if wanted properties can be checked
stringProperties = [];
for i = 1:size(properties,1)
stringProperties = [stringProperties; string(properties(i,1))];
end
if ~all(contains(stringProperties, possibleProperties))
error("Invalid Property. \nProperties can be:\n'XLabel'\n'YLabel'\n'ZLabel'\n'Title'", "");
end
%% Get line Array of current plot
handle = findobj(gca, 'Type', 'line');
%% Check if plot is existant
if sum(size(handle) > [0, 0]) == 0
error("No plot is existant.");
end
%% Cycle through each curve in the plot, break when one that pleases requirements is found
correctProperty = [];
for n = 1:size(properties,1) %% Create satisfaction array
correctProperty = [correctProperty; isPropertyCorrect(properties{n,1},properties{n,2})];
end
if all(correctProperty) %% Does line satisfy all requirents
result = true();
end
%% subfunction to determine if line has a correct property value
function isCorrect = isPropertyCorrect(property, desiredValue)
value = get(gca, property);
disp([property,' value="',value.String,'" desired value="', desiredValue,'"'])
if(strcmp(value.String,desiredValue))
isCorrect = 1;
else
isCorrect =0;
end
end
end
  2 Comments
Keith Hekman
Keith Hekman on 4 Feb 2022
Edited: Keith Hekman on 4 Feb 2022
thank you. The link didn't work for me though since it started on the previous line. Here is the correct link for people in the future.

Sign in to comment.


Peter Nagy
Peter Nagy on 2 Jun 2022
This is how I check for the properties of a plot. In the learner template I add locked lines that will check plot properties using 'get' that returns a cell array. Depending on what you write in the 'get' command, you can check different properties. In my example, I checked for the marker and line style, as well as the plotted data. Then, in the assessment part I compare the learner's solution to a reference cell array.
1. In the learner template I added these locked lines:
% ONLY WRITE ABOVE THESE LINES
phs=get(gca,'children');
p1propStudent=get(phs(1),{'Marker','LineStyle','XData','YData'});
2. In the assessment, I chose 'MATLAB code':
% Get reference solution
p1propRef=cell(1,4);
p1propRef{1}='none';
p1propRef{2}='-';
p1propRef{3}=[0 1 2 3 4 5 6 7 8];
p1propRef{4}=[-0.0839032873982978 3.21535644790528 6.51461618320886 9.81387591851244 13.1131356538160 16.4123953891196 19.7116551244232 23.0109148597267 26.3101745950303];
% Compare with learner solution.
assessVariableEqual('p1propStudent', p1propRef);

Communities

More Answers in the  Distance Learning Community

Categories

Find more on Vector Fields in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by