views:

200

answers:

2

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!

+3  A: 

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.

mtrw
except replace double-quotes with single-quotes
advs89
not bad - I'll wait and see if there's any better answers before I mark accepted though
advs89
@Adam - sorry about the quotes, fixed now. And I hope there's a better way!
mtrw
+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.
Andrew Janke
+3  A: 

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.

gnovice

related questions