Creating Checkboxes based on a while loop

Hello,
I am creating a system where it populates checkboxes based on a variable's value. (Using GUIDE mainly for GUI but to do this section, I am writing my own code) This is part of the code below:
while i < numOfElem
c(i) = uicontrol(panel,'Style','checkbox', 'String', elementTitles(i).title);
c(i).Position = [10 10 920-i.*50 920-i.*50];
i = i + 1;
end
numOfElem is the number of times it loops, as well as the number of times I would like the checkboxes to populate one underneath the other (with different strings -> elementTitles(i).title).
Each title appears with a checkbox, however, my method deletes the previous checkbox because I am overwriting it.
How can I solve this issue?
Thank you

 Réponse acceptée

Adam Danz
Adam Danz le 27 Juin 2018
Modifié(e) : Adam Danz le 27 Juin 2018
What units are you plotting in? Assuming 'panel' is the handle to the GUI figure,
panel.Units
It looks like you're using pixels but when you reposition the checkbox, you position if way off of the figure.
To test that, after this line in your code
c(i).Position = [10 10 920-i.*50 920-i.*50];
add this line to see if it re-appears (assumes units are pixels)
c(i).Position = [20 20 60 20]; % assumes figure size is default [403 246 560 420]

7 commentaires

yySBU
yySBU le 30 Juin 2018
Hello Mr. Danz,
The "panel" is the handler and it is referring to the panel I created in the figure. Below is a segment of code before the while loop that I haven't posted:
panel = handles.optPanel;
As for the position, it's in the corners of the figure but that is where I would like to place them so it is fine.
I'm still having the issue where the previous checkbox overwrites itself and erases in the while loop. How would I give it a different name as to not get them to erase each other? Shouldn't my variable "c" act as a matrix and store checkboxes in each "i" counter?
yySBU
yySBU le 30 Juin 2018
Sorry for my mistake but when I typed out panel.Units
it said "Characters"
Adam Danz
Adam Danz le 1 Juil 2018
Modifié(e) : Adam Danz le 1 Juil 2018
From the code you provided in your question, your checkbox handles (c(i)) are not being overwritten. To confirm that, run your code and your variable 'c' should be a vector of handles such that c{n} provides the n_th checkbox handle.
You say that the checkboxes are being positioned correctly. If that's the case your figure must be enormous because you are repositioning the checkboxes by enormous values in this line of your code.
c(i).Position = [10 10 920-i.*50 920-i.*50];
To confirm that, check out the matrix of your checkbox positions after your while loop:
checkboxPositions = cell2mat({c.Position}');
checkboxPositions =
10 10 120 870
10 10 120 820
10 10 120 770
10 10 120 720
10 10 120 670
10 10 120 620
10 10 120 570
10 10 120 520
10 10 120 470
and compare that to the original position of your checkboxes before you change the position (in units of characters) which is
[20 20 60 20].
Again, I think the problem is in your repositioning of the checkboxes or in the units you're using.
yySBU
yySBU le 1 Juil 2018
The output I got from checkboxposition is this:
10 10 870 870
10 10 820 820
10 10 770 770
10 10 720 720
10 10 670 670
10 10 620 620
10 10 570 570
10 10 520 520
10 10 470 470
10 10 420 420
10 10 370 370
10 10 320 320
10 10 270 270
10 10 220 220
10 10 170 170
10 10 120 120
Also, if I debug and stop at every loop, I can see each individual checkbox appearing in the correct spot (and work their way downwards as I pass by each loop) but it overwrites the previous box.
The checkboxposition values I posted were in pixel units, yours are in characters. I recreated the same data but used character units and I got the same position values you did. So, we're on the same page there. However, I don't see the checkboxes working their way across the figure as you report. So, there must be more to your code than what you shared. Could you upload a screenshot of your figure with the checkboxes working their way down the figure before disappearing?
Your line
c(i).Position = [10 10 920-i.*50 920-i.*50];
is repositioning them WAY off of my figure. You mentioned your figure units are characters. What is your figure position?
panel.Position
The figure must be very large if you can see the checkboxes being positioned up to 870 characters.
Here's why I say that... Your line
c(i) = uicontrol(panel,'Style','checkbox', 'String', elementTitles(i).title);
creates the checkbox in the lower, left corner of the figure by default and it has the default position of [3.8, 1.4615, ~~, ~~] (the width and height will depend on the string you used and font size, for more info on these position vectors, see this .)
Then you reposition it from (3.8, 1.4615) to (10,10) which is probably still on your figure, you increase the width and height from some small values to (870, 870) which is gigantic. The figure below shows a checkbox with position [10,10, 120, 8] and you can see how big the frame is around that position (the 4 boxes). Your sizes are up to 870 which is >7x larger than this. That ends up way off of my figure and I can no longer see it (though it still exists). Furthermore, the lower left corner of all of your checkboxes are at the position (10,10) which means they are all stacked on top of each other.
Please test this out in debug mode by executing these lines individually:
% Create the checkbox and record it's original position
c(i) = uicontrol(panel,'Style','checkbox', 'String', elementTitles(i).title);
originalPosition = c(i).Position;
% Reposition the checkbox and watch is disappear
c(i).Position = [10 10 920-i.*50 920-i.*50];
% Change the position back to original and see it reappear back where it was
c(i).Position = originalPosition;
yySBU
yySBU le 4 Juil 2018
Hello,
I finally figured out my problem. Yes, it was the positioning of the checkboxes and I realized it after debugging and coloring the foreground. The whole column had turned into one solid color which was why the checkbox background had been pasted over each other.
Thank you Mr. Danz for your help.
I changed my code to look like this:
c.Position = [10 450-i*20 60 20];
I was changing the checkbox sizes instead of the placements. (Even though the background of the checkboxes were the only thing that changed. The checkbox size itself didn't change.)
yySBU
yySBU le 4 Juil 2018
Picture mentioned above

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Graphics Object Properties dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by