Display default value in the edit box in MATLAB GUI

31 次查看(过去 30 天)
Hello,
I have a MATLAB GUI which contains the edit box.The value to this edit box is entered by the user . If the value is not entered by the user the default value has to be displayed or the previously set value has to be displayed in the edit box. Let me know how to set the default value to the edit box and display it if no value is entered , also how to store the value that is set . Previously set value has to be accessed and displayed if new value is not entered by the user.
It would be grateful if you let me know how this is done.
Looking forward to hear from you at the earliest.
Thanks
Pankaja

回答(2 个)

Walter Roberson
Walter Roberson 2015-7-25
Initialization
handles.prev_value = TheDefaultValue;
set(handles.edit_box1, 'String', handles.prev_value);
and callback
function edit_box1_callback(hObject, event, handles)
new_value = strtrim(get(hObject, 'String'));
if isempty(new_value)
new_value = handles.prev_value;
end
set(hObject, 'String', new_value);
handles.prev_value = new_value;
guidata(hObject, handles);
end

Shameer Parmar
Shameer Parmar 2016-6-14
Hello Pankaja,
You can do this, by creating new function in your code file (i.e. in .m file of your GUI).
1. Simply create new function at the end of your actual code as shown below:
function default(hObject, handles)
set(handles.edit1, 'string', 'WhateverYouWant_But_InStringFormat');
set(handles.edit2, 'string', 'WhateverYouWant_But_InStringFormat');
set(handles.edit3, 'string', 'WhateverYouWant_But_InStringFormat');
.
.
.
set(handles.edit100, 'string', 'WhateverYouWant_But_InStringFormat');
guidata(hObject, handles);
end
2. Call this function in the Opening function of your GUI as follows:
function xxxxxx_OpeningFcn(hObject, eventdata, handles, varargin)
% lines of code
.
.
default(hObject, handles);
.
.
guidata(hObject, handles);
end
You can call function "default()" wherever you want, in callback of any button/field of your GUI to make the text value default, as per your requirement.
Try for this and let me know if you face any issue

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by