Buttons to a matrix

Hello, I'm a novice at matlab and working on a college project. what im trying to do is have some sort of user interface, like the pic shown, that lets you select any or all of the buttons. this will represent a 5x5 matrix with either a 1 or a -1 (pressed, not pressed) then load it into the script for further calculations. is this possible/relatively simple?

1 Comment

doc guide
will let you lay them out yourself
doc uicontrol
with 'style', 'togglebutton' would create an individual object so you can place them in a grid mathematically using the 'Position' property.

Sign in to comment.

Answers (1)

Jan
Jan on 12 Oct 2017
Edited: Jan on 12 Oct 2017
It is "relative" simple - depending on how you define "simple".
Use either GUIDE or programmatically. Both methods have advantages. While in GUIDE you can create the buttons manually on the screen and drag them around like you want, you can use two loops to create this by code:
FigH = figure;
pos = [0, 0, 0.1, 0.1]; % [X, Y, Width, Height]
ButtonH = gobject(5, 5); % Pre-allocate
for i1 = 1:5
pos(1) = 0.05 + (i1 - 1) * 0.15;
for i2 = 1:5
pos(2) = 0.05 + (i1 - 1) * 0.15;
ButtonH(i1,i2) = uicontrol('Style', 'ToggleButton', ...
'Units', 'Normalized', 'Position', pos);
end
end
Now adjust the sizes and distances as you want.
[EDITED, Thanks Adam, ToogelButtons might be more useful for your case]

4 Comments

Adam
Adam on 12 Oct 2017
Edited: Adam on 12 Oct 2017
I would recommend using togglebutton rather than pushbutton though if you want 'pressed' or 'not pressed' state.
Personally I almost never use toggle buttons because I use checkboxes for the same behaviour, but if you specifically want buttons that is what they are for.
Jan
Jan on 12 Oct 2017
[MOVED from section for answers]
david smart wrote:
thanks for the help, i really appreciate it.
ive been playing with guide, a new feature to me. Ive gotten the buttons laid out and set them so that when they are selected they produce a 1 for the corresponding matrix coordinates, i think.
so i guess what im trying to do is have a 5x5 matrix default to all -1s and whichever toggle is selected go to 1 and then press the go button and run a script that uses hebb-net script ive written to identify which of the parameters the matrix fits. still working on it, but any help would be great.
Jan
Jan on 12 Oct 2017
Edited: Jan on 15 Oct 2017
@David: Please post comments in the section for comments, because it is confusing to have them as a new answer. Thanks.
It is hard to help you noew, because the descriptions are vague only. Start with deciding either for GUIDE or for a programmatic approach. Then proceed as far as you can and then come back to the foum and ask the next specific question.
will do, thanks and sorry. im new here

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 12 Oct 2017

Edited:

Jan
on 15 Oct 2017

Community Treasure Hunt

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

Start Hunting!