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

20 visualizaciones (últimos 30 días)
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?

Respuesta aceptada

Jeff Alderson
Jeff Alderson el 7 de Jul. de 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 comentarios
Jeff Alderson
Jeff Alderson el 1 de Oct. de 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 el 1 de Oct. de 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?

Iniciar sesión para comentar.

Más respuestas (2)

Keith Hekman
Keith Hekman el 4 de Feb. de 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 comentarios
Jeff Alderson
Jeff Alderson el 4 de Feb. de 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. https://www.mathworks.com/help/matlab/matlab_prog/function-argument-validation-1.html. 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 el 4 de Feb. de 2022
Editada: Keith Hekman el 4 de Feb. de 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.

Iniciar sesión para comentar.


Peter Nagy
Peter Nagy el 2 de Jun. de 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);

Comunidades de usuarios

Más respuestas en  Distance Learning Community

Categorías

Más información sobre Graphics Object Programming en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by