Adam
Adam

Reputation: 648

MATLAB: Print text in input field

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!

Upvotes: 1

Views: 1479

Answers (2)

gnovice
gnovice

Reputation: 125874

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.

Upvotes: 3

mtrw
mtrw

Reputation: 35098

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.

Upvotes: 4

Related Questions