How can you validate plot title, xlabel, and ylabel on a MATLAB Grader Assessment?
Show older comments
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
More Answers (2)
Keith Hekman
on 4 Feb 2022
I modified the code from https://www.mathworks.com/matlabcentral/fileexchange/78011-function-to-evaluate-plots-matlab-grader to check for titles and axis lables
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
Jeff Alderson
on 4 Feb 2022
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.
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);
2 Comments
Cris LaPierre
on 2 Jun 2022
This answer may also be of interest:
Attila
on 24 Sep 2024
This is a very interesting idea! I am actually working on exactly the same issue, thank you.
Communities
More Answers in the Distance Learning Community
Categories
Find more on Software Development 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!