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

11 次查看(过去 30 天)
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.

采纳的回答

Jon
Jon 2020-9-29
编辑:Jon 2020-9-29
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 个评论
Jon
Jon 2020-9-29
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.
Tore Henriksen Eliassen
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?

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Interactive Control and Callbacks 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by