In MATLAB Grader, how can I capture the printed output of a script type problem?

4 次查看(过去 30 天)
My understanding is that the expected way to capture the printed output of code is using evalc. I understand how I would do this for a function-type problem, but is there a name I can pass into evalc to call a learner's script for a script-type problem?
An example problem:
(a) Create a variable x=5
(b) Create a variable y=2*x, and display its value using the disp function. This should be the only output printed by your code.
I would like to be able to mark as incorrect submissions such as
x = 5
y = 2*x
disp(y)
to force students to learn how to use semicolons, etc.
I was envisioning something along the lines of
learner_output = evalc('learnerScript');
assert(strcmp(learner_output, reference_output))
  3 个评论
Benjamin Walker
Benjamin Walker 2024-3-30
Thank you for the suggestions - I will keep that in mind. It might come in useful for checking whether students use the colon operator or such correctly.
In my case here now that I have a working solution with evalc I think that will be the best option, especially because I don't want to make students include semicolons on lines that don't require them. Really more than semicolons it's about priming them to avoid unnecessary output to make it easier to teach the distinction between writing a function that returns a value, and writing a function that prints a value.

请先登录,再进行评论。

采纳的回答

Cris LaPierre
Cris LaPierre 2024-3-30
You are correct. The only way to do this in Grader is to use evalc.
For script-type problems, the learner solution is saved with the name solution.m (see here)
  1 个评论
Benjamin Walker
Benjamin Walker 2024-3-30
Thank you - that was exactly the info that I needed. I figured it must be in the documentation somewhere but it was a bit tucked away.
Almost as soon as I read your answer it came to me that I could use a command line
assert(false, ls)
within a test to view the file structure directly, which helped me further learn that the reference solution is stored as "+reference/solution.p", so that the full solution for what I wanted is something like
learner_output = evalc('solution');
reference_output = evalc('reference.solution')
assert(strcmp(learner_output, reference_output), "Output is incorrect")
Hopefully this is useful to anyone else trying to do something similar.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

产品


版本

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by