1

Using MATLAB,

I have this code:

value = input('>> Enter a value: ');

and basically, I want a "default" value to the right of the colon

(sortof like this)

>> Enter a value: 12

where "12" is editable such that the user could [backspace] [backspace] and change the value to, say, "20" or something.

Is there any (easy) way to do this?

Thanks!

2 Answers 2

4

You can hack the behavior, though not the look, with:

myDefault = 12;
x = input(['Enter a value (press Enter for default = ' num2str(myDefault) ')']);
if (isempty(x))
    x = myDefault;
end

Ugly, but I don't know of a simpler way.

3
  • except replace double-quotes with single-quotes
    – Adam
    Apr 8, 2010 at 23:42
  • @Adam - sorry about the quotes, fixed now. And I hope there's a better way!
    – mtrw
    Apr 8, 2010 at 23:46
  • +1. Lots of interactive Unix shell scripts work this way. The convention there is to put the default value in square brackets ("[]"). If you're feeling adventurous, you might be able to hack some Java AWT code up to grab the Command Window's text entry field and enter the default value by doing simulated typing after input() posts its prompt. But that's probably not worth the effort. Apr 9, 2010 at 14:04
3

You could always go the GUI route and use the function INPUTDLG to create a dialog box, as discussed in this MathWorks blog post. For example:

b = inputdlg('What kind of Peanut Butter would you like?');

Will create the following dialog box:

alt text

You can easily add default values for the inputs. Here's a dialog box for your example:

value = inputdlg('Enter a value:','Input',1,{'12'});

There are also many other types of built-in dialog boxes that you can choose from.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.