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

15 visualizzazioni (ultimi 30 giorni)
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?

Risposta accettata

Jeff Alderson
Jeff Alderson il 7 Lug 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 Commenti
Jeff Alderson
Jeff Alderson il 1 Ott 2020
You are correct. While you can create a custom MATLAB code type assessment to evaluate pretty much anything, we don't have a built in assessment type for checking equivalence of plot objects. It's something that we are working on - to allow the instructor to choose parameters that they want to compare without writing code.
Justin Wyss-Gallifent
Justin Wyss-Gallifent il 1 Ott 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?

Accedi per commentare.

Più risposte (2)

Keith Hekman
Keith Hekman il 4 Feb 2022
result = double(r);
%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", "");
%% 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", "");
%% Generate cell of searched for properties from varargin
if length(varargin) == 2
properties = varargin;
properties = reshape(varargin, [2, length(varargin)/2])';
%% Check if wanted properties can be checked
stringProperties = [];
for i = 1:size(properties,1)
stringProperties = [stringProperties; string(properties(i,1))];
if ~all(contains(stringProperties, possibleProperties))
error("Invalid Property. \nProperties can be:\n'XLabel'\n'YLabel'\n'ZLabel'\n'Title'", "");
%% 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.");
%% 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})];
if all(correctProperty) %% Does line satisfy all requirents
result = true();
%% 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,'"'])
isCorrect = 1;
isCorrect =0;
  2 Commenti
Jeff Alderson
Jeff Alderson il 4 Feb 2022
This is an interesting approach. If this is something that you feel necessary for your code - checking parameters of a plot object, I would suggest a couple improvements:
  • Take a look at MATLAB support for function argument validation. Basically, use the new feature introduced to MATLAB to check if the arguments passed in are expected, formatted, and match the pattern you wish. This could save you quite a few lines of code at the top of your function definition.
  • Package your mg_isLabelInPlot function, or P-code it, and put it in the Files Referenced section of the problem definition. This will allow you to call the function from multiple assessment tests without needing to put the function in every assessment test code block.
Keith Hekman
Keith Hekman il 4 Feb 2022
Modificato: Keith Hekman il 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.

Accedi per commentare.

Peter Nagy
Peter Nagy il 2 Giu 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:
2. In the assessment, I chose 'MATLAB code':
% Get reference solution
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);


Più risposte nel  Distance Learning Community


Scopri di più su Graphics Object Programming in Help Center e File Exchange


Community Treasure Hunt

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

Start Hunting!

Translated by