How do i make a uipanel with checkboxes return information to my script?

Bottom line im trying to get my uifigure checkboxes to create a logical vector(f) that consistis of zeroes or ones based on what checkboxes gets selected.
All the files needed to run the code is attached, the BoxChanged.m function changes the value of the checkbox depending on wheter or not the respective box is selected(i think)
further than that i cant seem to get tho, considering i dont know how to use this to display information.
this function creates vectors simulating a 6 dice dicethrow
function [result] = terning(d,ant)
%TERNING Playing a random number of same sided dice
% Creating a result vector for the played dice. Taking the inputs d and
% ant to determine what results we get when we play ant d-sided dice.
result = randi(d,1,ant);
end
here is the function creating checkboxes:
function [box] = hukboks(a,b,y,p)
%This funcion creates a Checkbox in uifigure p, where b is a placeholder
%for hom many boxes needs to be made, and a is a counter telling the
%function how many boxes has been made, y is a variable setting names for
%the boxes
%S is the size of the checkbox(big enough for 1 digit)
%v is the spacing in pixels for left to right of the figure
%h is the distance(in pixels) from the bottom of the figure to the row of
%checkboxes
v = 700.*(a/b);
h = 350;
s = 25;
n = num2str(y);
box = uicheckbox(p,'text',n,'Position',[v h s s],...
'ValueChangedFcn',@(box,event) BoxChanged(box));
end
this function checks the value of the checkboxes(if the changed or not)
function BoxChanged(bx)
%Determining what will happen when Checkboxes are selected
val = bx.Value;
if val
bx.Value = 1;
else
bx.Value = 0;
end
end
this is the script i made to try to create my output vector f
p = uifigure('Name','PLZ HELP','Position',[150 100 909 539]);
kastvektor = terning(6,6);
b = length(kastvektor);
f = zeros(1,b);
for a = 1:b
y = kastvektor(a);
if a == 1
box1 = hukboks(a,b,y,p);
end
if a == 2
box2 = hukboks(a,b,y,p);
end
if a == 3
box3 = hukboks(a,b,y,p);
end
if a == 4
box4 = hukboks(a,b,y,p);
end
if a == 5
box5 = hukboks(a,b,y,p);
end
if a == 6
box6 = hukboks(a,b,y,p);
end
end
As explained earlier im trying to convert the zeroes vector f to become a vector of ones and zeroes corresponding to inputs from my panel.
Attached is also how the panel may look with boxes 1 to 6 going from left to right
Please if you know anything or have tips let me know im kinda smashing my heasd against a wall here.

 Accepted Answer

I have attached a modified script to generate your ui, which I have called checkboxGo.
Note that in this I have used a simple loop to create the checkboxes. This replaces your long sequence of if statements.
I also modified the callback function so that everytime a checkbox is clicked on it displays a vector with the state of all of the checkboxes.
I'm not sure quite what you are trying to do here, but hopefully this gets you close.
Please let me know if you have further questions.
CheckboxGo.m script
% set some parameters
b = 6; % number of checkboxes
% open up a figure to put the checkboxes in
p = uifigure('Name','PLZ HELP','Position',[150 100 909 539]);
% assign initial random values for check boxes
kastvektor = terning(6,6);
% loop to create checkboxes inside of the figure
box = zeros(b,1); % preallocate array to hold handles to checkboxes
for a = 1:b
y = kastvektor(a);
box(a) = hukboks(a,b,y,p);
end
Callback for checking boxes
function BoxChanged(bx)
%Determining what will happen when Checkboxes are selected
% get handles to all of the check boxes
% parent of bx is UIPanel where checkboxes are located
% children of this UIPanel is an array of checkboxes
boxes = bx.Parent.Children;
% get the state (checked or not checked) of all the boxes
boxStates = [boxes.Value]; % square brackets puts them into a vector
% display the current vector
display(boxStates)

6 Comments

this gets me rly close(i think), im kinda trying to make a panel that allows you to choose any number of boxes and then lets you press a button or a sepearate box wich then closes the panel and returns a vector consisting of either:
ones and zeroes or
the numbers/names of the diffrent boxes that are checked
is that possible?
and does this make sense?
thanks for the help
Do you really need to pass the data back to a script? I'm a little stuck on how you would do this. Usually you would make a little app using appdesigner. Then all of the components, e.g. check boxes etc can share data using the app properties. You can add properties for this purpose.
Another thought, I think you can probably do what you want using a dialog function please see https://www.mathworks.com/help/matlab/ref/dialog.html
Here is a skeleton of how you could do it with a dialog.
Note the hukboks function has been changed, make sure not to get confused with the old one. Also the BoxChanged function is no longer used. It is replace by the nested callback box_changed inside of checkBoxDialog
Modified hukboks function:
function [box] = hukboks(a,b,y,p,callback)
%This funcion creates a Checkbox in uifigure p, where b is a placeholder
%for hom many boxes needs to be made, and a is a counter telling the
%function how many boxes has been made, y is a variable setting names for
%the boxes
%S is the size of the checkbox(big enough for 1 digit)
%v is the spacing in pixels for left to right of the figure
%h is the distance(in pixels) from the bottom of the figure to the row of
%checkboxes
v = 700.*(a/b);
h = 350;
s = 25;
n = num2str(y);
% % box = uicheckbox(p,'text',n,'Position',[v h s s],...
% % 'ValueChangedFcn',@(box,event) BoxChanged(box));
box = uicontrol('Parent',p,'Position',[v h s s],...
'Style','checkbox','Callback',callback);
end
function to launch dialog:
function boxStates = checkBoxDialog(numCheckbox)
% creates dialog to get user check box selections
b = numCheckbox;
% open the dialog
dlg = dialog('Position',[150 100 909 539],'Name','Select Boxes');
% assign initial random values for check boxes
kastvektor = terning(6,6);
% loop to create checkboxes inside of the dialog
box = zeros(b,1); % preallocate array to hold handles to checkboxes
for a = 1:b
y = kastvektor(a);
% create each box
box(a) = hukboks(a,b,y,dlg,@box_changed);
end
% wait for dialog to close
uiwait(dlg)
% checkbox callback function, nested so checked is in main function
% workspace too
function box_changed(bx,~)
%Determining what will happen when Checkboxes are selected
% get handles to all of the check boxes
% parent of bx is dialog where checkboxes are located
% children of this dialog is an array of checkboxes
boxes = bx.Parent.Children;
% get the state (checked or not checked) of all the boxes
boxStates = [boxes.Value]; % square brackets puts them into a vector
end
end
Script to run it (or just type these few lines on command line):
% set some parameters
b = 6; % number of checkboxes
% open the checkbox dialog and get data back
boxState = checkBoxDialog(b)
Note in my simple example you just "x" out of the dialog window that is click the x in the upper right corner to close it and your selection is then read. You will probably want to add a button whose callback closes the dialog.
okei this is more or less exactly what im looking for as i am trying to make the dicegame 10 000, dont know if you are familiar?
so as long as this again can work and or be called multiple times in a script or something to get a varying display, dont know if i actually then have to use something other than checkboxes and panels.
Because the idea is that it to some degree will save the numbers of the chosen boxes to one vector and reroll numbers of unchosen boxes for then returning to the window with that amount of boxes in them, with names according to the diffrent dice rolled.
does this make sense?

Sign in to comment.

More Answers (0)

Categories

Find more on Develop Apps Programmatically in Help Center and File Exchange

Products

Release

R2020a

Community Treasure Hunt

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

Start Hunting!