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

17 ビュー (過去 30 日間)
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?

採用された回答

Jeff Alderson
Jeff Alderson 2020 年 7 月 7 日
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 件のコメント
Jeff Alderson
Jeff Alderson 2020 年 10 月 1 日
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 2020 年 10 月 1 日
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?

サインインしてコメントする。

その他の回答 (2 件)

Keith Hekman
Keith Hekman 2022 年 2 月 4 日
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 件のコメント
Jeff Alderson
Jeff Alderson 2022 年 2 月 4 日
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 2022 年 2 月 4 日
編集済み: Keith Hekman 2022 年 2 月 4 日
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
Peter Nagy 2022 年 6 月 2 日
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 件のコメント
Attila
Attila 2024 年 9 月 24 日
This is a very interesting idea! I am actually working on exactly the same issue, thank you.

サインインしてコメントする。

コミュニティ

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by