Filter löschen
Filter löschen

Can i set all text boxes to their default value by using only one command

10 Ansichten (letzte 30 Tage)
I 'm using this command to set textbox to its default value I have 17 text boxes in my GUI. I want to reset all of them by using command only one time instead of calling this 17 times. Is it possible?
set(handles.text1, 'string', 'default');

Akzeptierte Antwort

Shameer Parmar
Shameer Parmar am 14 Jun. 2016
Hello Mania,
You can achieve your requirement, 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.text1, 'string', 'WhateverYouWant_But_InStringFormat');
set(handles.text2, 'string', 'WhateverYouWant_But_InStringFormat');
set(handles.text3, 'string', 'WhateverYouWant_But_InStringFormat');
.
.
.
set(handles.text17, '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 function 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.
  1 Kommentar
clear all
clear all am 27 Feb. 2017
Bearbeitet: Walter Roberson am 27 Feb. 2017
Thank you so much for your help.
I tried and it works(the edit box shows default vaule).
But the problem is that I have to click(or press enter) the edit box to ensure that the box has value.
If I do not click the edit box, then edit box do not perceive the default value.
It would be grateful if you let me know how this is done.
Thanks
This is my code.
% ================================================================
function aaaaaGUI_OpeningFcn(hObject, eventdata, handles, varargin)
% lines of code
...
...
default_aaaaa(hObject, handles);
...
...
guidata(hObject, handles);
% ================================================================
function edit_ac_Callback(hObject, eventdata, handles)
global critical_aaaaa
critical_aaaa = str2double(get(handles.edit_aaaaa,'string'));
% ================================================================
function edit_aaaaa_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% ================================================================
function default_aaaaa(hObject, handles)
set(handles.edit_aaaaa,'string','500')
guidata(hObject, handles);

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Jos (10584)
Jos (10584) am 14 Jun. 2016
This task would be utterly trivial if you had put the handles to the boxes into an array on creation!
handles.text(1) = ..
handles.text(2) = ..
...
set(handles.text,'String','') ;
  1 Kommentar
Stephen23
Stephen23 am 27 Feb. 2017
Bearbeitet: Stephen23 am 27 Feb. 2017
Yes, this is the best answer! When graphics handles are explicitly stored and accessed then the code will be clearer, faster, and more robust.
Avoid the problem by writing better code in the first place:

Melden Sie sich an, um zu kommentieren.


Walter Roberson
Walter Roberson am 17 Mai 2016
In MATLAB, textbox objects do not have a default value; or if they do, it is the empty string. textbox objects are also used for output, not for input; do you perhaps mean editbox?
set( findall( ancestor(handles.edit1, 'figure'), 'style', 'edit'), 'string', 'WhatEverYouWant')
  3 Kommentare
mania online
mania online am 18 Mai 2016
This is not working. I want to set all static textboxes to their default value by a single command.
Walter Roberson
Walter Roberson am 18 Mai 2016
set( findall(0, 'style', 'edit'), 'string', '')
The default value of a uicontrol of style edit is the empty string.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Migrate GUIDE Apps finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by