Clear Filters
Clear Filters

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

1 view (last 30 days)
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 Comments
Benjamin Walker
Benjamin Walker on 30 Mar 2024
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.

Sign in to comment.

Accepted Answer

Cris LaPierre
Cris LaPierre on 30 Mar 2024
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 Comment
Benjamin Walker
Benjamin Walker on 30 Mar 2024
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!