Creating 1024 ToggleButtons with almost the same callback
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Benedikt Schwarz
le 19 Août 2019
Commenté : Benedikt Schwarz
le 21 Août 2019
Hey together,
For a 1024 LED-Matrix I wanted to create a GUI where u can swith every single pixel on and off with a certain brightness. I wanted to create toggle Buttons which switch the Pixel on/off. So I started creating a 1024 huge buttonmatrix in GUIDE with copy/pasting as much buttons as possible. Afterwards to make sure Pixel 1 really represent Pixel 1 I renamed all Buttons accrouding to their Pixelnumber. EDIT : The Tag goes like T#### , for example for Pixel 1020 T1020 and for Pixel 66 T66.
For the callback I wrote the following Method to just copy/paste in every callback :
global TData
brtn = get(handles.slider1, 'Value');
Tag = get(hObject, 'Tag');
PPos = 0;
Si = size(Tag);
for i = 2:Si(2)
PPos = PPos*10 + str2num(Tag(i));
end
if(brtn < 0.3)
brt = 0.3;
else
brt = brtn;
end
if(get(hObject,'Value') == 1)
set(hObject,'BackgroundColor',[1*brt 1*brt 0]);
TData(PPos) = 256 * brtn;
elseif (get(hObject,'Value') == 0)
set(hObject,'BackgroundColor',[0.149 0.149 0.149]);
TData(PPos) = 0;
end
So my question is it somehow possible to make this "smarter" with a Buttongroup etc. or do I really just have to copy/paste this 1024 times again?
Thanks for your answers!
0 commentaires
Réponse acceptée
Jan
le 19 Août 2019
Modifié(e) : Jan
le 19 Août 2019
A simplified version of your code:
function buttonPress(hObject, EventData, handles)
global TData
brtn = get(handles.slider1, 'Value');
Tag = get(hObject, 'Tag');
PPos = sscanf(Tag, 'T%d');
brt = max(0.3, brtn);
if get(hObject,'Value')
set(hObject,'BackgroundColor', [brt, brt, 0]);
TData(PPos) = 256 * brtn;
else
set(hObject,'BackgroundColor', [0.149, 0.149, 0.149]);
TData(PPos) = 0;
end
You can use the same callback for all buttons. There is no need to create a specific function for each button, because the code uses the tag to perform the different actions.
By the way, global variables are a mess. Prefer to use guidata to store the values in the figure's ApplicationData. This avoid collisions with other programs and allows for a clean debugging of your code.
I prefer to create figures by code instead of using GUIDE. 1024 almost equal objects are easier to create in a loop:
FigH = figure;
index = 0;
shift = [0.0005, 0.0005, -0.001, -0.001];
for iy = 1:32
for ix = 1:32
index = index + 1;
uicontrol(FigH, 'Style', 'ToggleButton', ...
'String', sprintf('%d', index), ...
'Units', 'normalized', ...
'Position', [ix-1, iy-1, 1, 1] / 32 + shift, ...
'Callback', {@buttonPress, handles, index});
end
end
function buttonPress(hObject, EventData, handles, index)
PPos = index;
... see above
end
9 commentaires
Jan
le 20 Août 2019
The message tells you, that the callback function is called with too few input arguments. Instead of
set(handles.(['T' num2str(i)]), 'Callback', @pixel_Callback);
write:
set(handles.(['T' num2str(i)]), 'Callback', {@pixel_Callback, handles});
If you want to consider my recommendations, replace this by:
for i = 1:1024
set(handles.(['T' num2str(i)]), 'Callback', {@pixel_Callback, handles, i});
end
and simplify the pixel_Callback function:
function pixel_Callback(hObject, eventdata, handles, PPos)
global TData
brtn = get(handles.slider2, 'Value');
brt = max(0.3, brtn);
if get(hObject,'Value')
set(hObject,'BackgroundColor', [brt brt 0]);
TData(PPos) = 256 * brtn;
else
set(hObject,'BackgroundColor',[0.149 0.149 0.149]);
TData(PPos) = 0;
end
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Migrate GUIDE Apps dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!