Cannot call INPUT from EVALC

Hey everyone!
I'm working on a MATLAB project which involves user input. The actual project itself all works fine up until I call input(), in which it breaks and continues to run but gives me the error
"Error using input
Cannot call INPUT from EVALC.
<Location of file here>
t = input('enter a value of t')"
I tried to run the program by just clicking "run" and it works fine, but when I try to publish it it gives me this error. This function used to work in publish mode, too, but it just stopped working at some point.
To test, I made a file called "Test.m" where I just tried an input and printed it:
test = input('input');
display(text)
This worked fine when I pressed run but when I tried to publish it it didn't work and gave the same error. This made me assume that it was something wrong with my publish settings so I tried factory resetting the publish settings and it kept giving me the error (for reference here are my settings)
I've been trying to debug this for a while, but still no avail. I also tried the same code in matlab online, and its the same thing. Does anyone know why this might be?
Thank you!

1 Kommentar

Mehri Mehrnia
Mehri Mehrnia am 30 Mai 2022
"EVALC" errors may relate to bad function name. I'm not optimistic with "Test" name!

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Steven Lord
Steven Lord am 19 Nov. 2020

0 Stimmen

You cannot call input from within evalc. I believe this is the expected behavior. evalc would prevent the prompt text from input from displaying, thus giving you no indication that the user was expected to type anything.
I recommend splitting your code into two pieces. The first is the computational function that accepts input arguments as input arguments (not typed in at an input prompt) and can be published (which uses evalc to capture the output to be included in the published document.) The second is an interface script or function that can call input and calls the computational function with inputs entered at the input prompt. You would not try to publish this second piece.
function computationalPiece(k)
x = 0:360;
y = sind(k*x);
plot(x, y)
end
and
function interfacePiece
k = input('Enter the value of k: ');
computationalPiece(k)

5 Kommentare

Lincoln Doney
Lincoln Doney am 19 Nov. 2020
It actually is in two different functions right now.. how does this explain it not working with my test file that only asked for the input but nothing else though?
Steven Lord
Steven Lord am 19 Nov. 2020
If you call a function inside evalc that function cannot display anything to the Command Window; evalc captures that output.
So how exactly would the user of your code know that input tried and failed to prompt you to enter anything? It would appear to the user that MATLAB had stalled. To prevent giving the user the appearance of MATLAB stalling, input is not allowed inside evalc.
As an analogy, suppose you sent your friend Alice an email asking "Where should we go for dinner?" That message got caught in Alice's spam folder. At some point in the not-to-distant future you call Alice up and ask "Well, where should we go for dinner? I'm hungry!" Alice never receieved the question and so never answered, and that made you upset.
Lincoln Doney
Lincoln Doney am 19 Nov. 2020
I think I get it... I don't call evalc anywhere though, is that something inherent to publishing?
Steven Lord
Steven Lord am 19 Nov. 2020
Publishing needs to capture whatever your function displays as part of its execution so it can put that into the report being published. The way it does that uses evalc.
Dhruti
Dhruti am 28 Mär. 2024
Bearbeitet: Dhruti am 28 Mär. 2024
I don't think this works. There's still error during publishing.
Also, we have to add 'end' after using 'function'.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Startup and Shutdown finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 19 Nov. 2020

Bearbeitet:

am 28 Mär. 2024

Community Treasure Hunt

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

Start Hunting!

Translated by