Azzera filtri
Azzera filtri

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

4 visualizzazioni (ultimi 30 giorni)
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 Commenti
Benjamin Walker
Benjamin Walker il 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.

Accedi per commentare.

Risposta accettata

Cris LaPierre
Cris LaPierre il 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 Commento
Benjamin Walker
Benjamin Walker il 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.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Prodotti


Release

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by