Problem with checkbox in Gui

Hi,
I am creating a GUI that is having checkbox. I used the uicheckbox since everytime that I am running the gui the number of checkboxes will be changed. The program first read from a csv file and according to the rows in the csv file, the checkbox will be created so that user can choose from these checkboxes.
The problem is that when I am running the program out of gui it is working perfect and the checkbox is a matlab.ui.control.checkbox. But when I am using it inside the gui, it will be a double and I can not find which ones user selected.
Can anyone help??

9 Commenti

We will need to see your code.
faran
faran il 11 Lug 2018
Modificato: faran il 11 Lug 2018
Below is my code. First it will read the txt and find how many cells are non empty and then it will create checkbox. since I had many nonempty cells I organized it to be 20 checkbox in each column.
I=find(~cellfun(@isempty,txt(:,1)));
lastnonemptycell=I(end);
num_column=round((lastnonemptycell-1)/20)+1;
Target_Library=txt(2:lastnonemptycell,1);
Target_fig = uifigure('Name','Target Library','Position',[100 100 150*num_column 650]);
for col=1:num_column
try
for i=1:20
Target_checkbox(((col-1)*20)+i) = uicheckbox(Target_fig, 'Text',Target_Library{((col-1)*20)+i,1},...
'Value', 0,...
'Position',[15+150*(col-1) 520-(i-1)*30 100 100]);
continue;
end
end
end
Geoff Hayes
Geoff Hayes il 11 Lug 2018
Sara - why do you have a continue in your inner for loop. Won't this just create one checkbox for that column before exiting the inner loop and moving to the next column?
Please also clarify what you mean by .. I am running the program out of gui it is working perfect... What program are you running outside of the GUI? What is the double when running the code in side the GUI?
faran
faran il 11 Lug 2018
Geoff, this is the code that I am using both for the inside the gui and outside by itself. When I am running it out of Gui the output which is "Target_checkbox" will be a matlab.ui.control.checkbox but when I am running it inside the Gui, the output will be double numbers which is not showing which checkbox was selected.
I put the continue since matrix dimension will missmatch and it will show an error which is ok, so continue will let the program know to ignore the error and continue. it will make the checkbox correctly.
When you are running it inside the GUI, is it possible that you are running it in R2014a or earlier?
faran
faran il 11 Lug 2018
No I am using R2017b
Geoff Hayes
Geoff Hayes il 11 Lug 2018
Sara's answer moved here
I tried the uicontrol method too but still the output is a matrix!
Inside the gui: is it possible the GUI is App Designer based?
... I think we are going to need the GUI code (and any .fig) to test with.
faran
faran il 11 Lug 2018
Here is a smaller version of the gui. When you generate it and check the output you will see it is a matrix while if you just run the codes out of gui you will see that output is a matlab.ui.control.checkbox.

Accedi per commentare.

Risposte (1)

In testgui_OpeningFcn add the lines
global Target_checkbox
Target_checkbox = gobjects(0);
What is happening is that you are not initializing Target_checkbox, so by default it is initialized to [] which is a numeric array. When you assign a graphics handle into a double array, it automatically converts it to the old-style numeric handles.

6 Commenti

faran
faran il 11 Lug 2018
Thank you Walter, it worked perfect. Just one more question. Is there any command that save the information of the checkbox so that when I am closing the figure, it will save the information of which checkbox were selected? When I am closing the figures, the information of the checkbox will be deleted!
selected = cell2mat(get(Target_checkbox,'Value'));
faran
faran il 11 Lug 2018
Thanks but it will give the initial value, it will not get updated with the selected.
I tested it, and it will always return the values as they are at the time you run the line of code.
Do you know if this samemethod would work for a property?
Initializing to gobjects() would be useful for a property that will eventually be a graphics object.

Accedi per commentare.

Categorie

Scopri di più su Creating, Deleting, and Querying Graphics Objects in Centro assistenza e File Exchange

Richiesto:

il 11 Lug 2018

Commentato:

il 12 Ago 2021

Community Treasure Hunt

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

Start Hunting!

Translated by