4

I would like to publish a Matlab code, which needs some input from the user (a single string). The code runs fine, but when I am trying to publish it I get

Error using input
Cannot call INPUT from EVALC.
Error in test (line 185)
userinput = input('Enter the code here\n', 's');

Is there any workaround?

3
  • Sorry about the strange code markup, it's probably not intended for Matlab. Commented Dec 9, 2011 at 12:27
  • 1
    What do you mean by "publish"? Commented Dec 9, 2011 at 12:48
  • Publishing is a special Matlab feature. Usually File > Publish or publish(mfilename). Only scripts may be published. Commented Dec 9, 2011 at 17:52

4 Answers 4

5

If you click the little drop-down menu next to the publish button, you'll see an item called Edit Publish Configurations. If you select that (while the file you want to publish is open in the editor), you'll see you have a MATLAB expression that you can edit. This is the MATLAB code that gets executed when you publish the script, and by default it's just the name of your script.

Let's say your script is called myscript, so by default the published expression is just

myscript

You can edit the expression to be:

userinput = 'example';
myscript

Then this will be run when you hit the publish button.

Within your script, change the line

userinput = input('Enter the code here\n', 's');

to

if ~exist('userinput', 'var')
    userinput = input('Enter the code here\n', 's');
end

Now your script will run as normal (assuming you don't have a variable 'userinput' in the workspace), and your script will publish successfully.

Hope that helps!

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for the tip but unfortunately the user input exactly does change from run to run.
Publishing isn't a good fit if the script specifically requires interactive user input. You'll need to use some sort of workaround. If it's not the one above, how about removing the input command from your script, and creating a second script that calls input followed by the publish command on your original script? Then run that.
I am trying to implement OAuth in Matlab. It requires user input of the verification string, provided by the server, into the program. The string is only provided after the first half of the code and is needed for the second half.
I did find a workaround, but a rather brute-force one. I am publishing the code without executing it, then execute it and just add the output to the published material.
Glad you found a workaround! Will you be sharing your MATLAB OAuth implementation when it's ready? There are a lot of cool things you could do with that - good luck with it.
|
1

If you want to publish your matlab file (xyz.m) without executing the code , then use the following.

% To stop executing the script before publish

opts.evalCode = false

% Similarly here are some more options 

% If you want to publish only comments and not code, then 

opts.showCode = false

% Publish doc format, html by default

opts.format = 'html'

% Catch error before publishing

opts.catchError = true

% Publish doc output path

%opts.outputDir = <path>

% Finally publish 

publish('xyz.m',opts)

Comments

1

Instead of using the input command use inputdlg instead. So you would have;

userinput = inputdlg('Enter the code here:');

This creates a dialog box generating the inputted values as a string and allows you to execute and publish the code

Comments

0

There is no direct way of using input() in a code you want to execute & publish the results. Workarounds are possible. I would simply split the code into two (or more) parts. First part will use input() and then publish(). The other part(s) will be the actual code while the first part forms a wrapper on second. The publish() in first part would call the second (and/or other) part(s). This gives flexibility to control what's to be shown in the generated output to properly indicate that input() is being used.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.