Hi everyone,
I am working with checkbox in GUI and I have found that checkbox remains blank at the initial stage.
I need to know how to set the checkbox ticked so that no need to put a checkmark manually in the checkbox.
Any suggestion will be appreciated.

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 18 Sep. 2015

1 Stimme

Set the Value property to 1.
uicontrol('Style','check','Value',1)

Weitere Antworten (3)

Siam
Siam am 18 Sep. 2015

2 Stimmen

Another option could be to use
set(handles.checkbox,'value',1) where 1 for checked and 0 for unchecked.

3 Kommentare

Tania
Tania am 19 Sep. 2015
Working for me. Thank you both.
Lokendra Agrawal
Lokendra Agrawal am 30 Jun. 2020
i think in value the first letter should be capital so it will be set(handles.checkbox,'Value', 0);
@Lokendra: properties are not case sensitive. You don't even need to finish typing it, as long as you have a unique match:
figure(1),clf(1)
h_=plot(rand(1,4));
hold on,plot(rand(1,4))
set(h_,'visi','off')
If you use the object notation you do need to be precise:
figure(1),clf(1)
h_=plot(rand(1,4));
hold on,plot(rand(1,4))
h_.Visible='off';

Melden Sie sich an, um zu kommentieren.

Ali Afruzi
Ali Afruzi am 5 Feb. 2021

0 Stimmen

You can use the following for your GUI
% --- Executes on button press in DataEaseInput.
function YourCheckbox_Callback(hObject, eventdata, handles)
% hObject handle to YourCheckbox (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes during object creation, after setting all properties.
function YourCheckbox_CreateFcn(hObject, eventdata, handles)
% hObject handle to YourCheckbox (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
set(hObject,'value',1)

3 Kommentare

Rik
Rik am 6 Feb. 2021
This code is only relevant for GUIDE-created GUIs. It also contains only 1 actual line of code. That code also exists in both other answers. So what does this add?
For general advice and examples for how to create a GUI (and avoid using GUIDE), have look at this thread.
I added the comment becasue it clarifies where
set(hObject,'value',1)
must be used. 'set(hObject,'value',1)' must be used in CreateFcn function and not in Callback function.
Rik
Rik am 6 Feb. 2021
Maybe I'm getting too familiar with the way GUIs work, but isn't it obvious it needs to be called when you create the object or start your GUI? If you put it in the callback function, it would happen every time you interact with the checkbox.

Melden Sie sich an, um zu kommentieren.

Arlon
Arlon am 24 Apr. 2024

0 Stimmen

app.NameOfCheckbox.Value=true; % (or 1) (in startupFcn function)

Kategorien

Mehr zu Creating, Deleting, and Querying Graphics Objects finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 18 Sep. 2015

Beantwortet:

am 24 Apr. 2024

Community Treasure Hunt

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

Start Hunting!

Translated by