Info

Cette question est clôturée. Rouvrir pour modifier ou répondre.

"Activate" lines with a GUI?

1 vue (au cours des 30 derniers jours)
mec123
mec123 le 27 Juil 2016
Clôturé : MATLAB Answer Bot le 20 Août 2021
Hi,
i am very new to Matlab so please no blame for "bad" questions.
What I have is a script to do some calculations and display the results afterwards in separate windows. Since i never need all results at the same time, i created a GUI with checkboxes and a "run" button. This way i want to reduce the high calculation time.
My first target is the following:
With the checkboxes i want to determine, which commands / parts of the script are supposed to be run. Triggering the boxes must not activate anything before the "run" button is clicked. Hitting the "run"-Button activates the script like usual, just with less commands.
So instead of
1
2
3
4
5
i want to get something like
1
2
3
checkbox -> 4
checkbox -> 5
checkbox -> 6
To start with i want to be able to switch on/off "figure"-commands.
I hope it is roughly clear what i'm trying to tell.
Thanks
  2 commentaires
Stefan Reich
Stefan Reich le 28 Juil 2016
How about an if/else in the pushbotton_Callback function?
mec123
mec123 le 28 Juil 2016
Thank you! An if/else in the pushbutton reffering to the results of the if/else's of the checkboxes.

Réponses (1)

Stephen23
Stephen23 le 28 Juil 2016
Modifié(e) : Stephen23 le 29 Juil 2016
This is easy using nested functions (tested on MATLAB 2012b):
function temp0
%
F = figure();
P = uipanel(...
'Parent',F,...
'Units','normalized',...
'Position',[0.1,0.1,0.7,0.6]);
%
N = 5;
V = 0:N;
H = V;
gap = 0.1;
hgt = (1-(N+1)*gap)/N;
for k = 1:N
H(k) = uicontrol(...
'Parent',P,...
'String',sprintf('Option %d',k),...
'Style','checkbox',...
'Units','normalized',...
'Position',[0.1,gap+(gap+hgt)*(N-k),0.8,hgt]);
end
%
uicontrol(...
'Parent',F,...
'String','Run',...
'Style','pushbutton',...
'Units','normalized',...
'Position',[0.1,0.8,0.7,0.1],...
'Callback',@MyFun);
%
function MyFun(~,~)
for n = 1:N
if get(H(n),'Value')
fprintf('Option %d has been selected\n',n)
... your code here
end
end
end
%
end
And produces this figure with several checkboxes:
And when "run" is clicked it prints this in the command window
>> temp0()
Option 1 has been selected
Option 3 has been selected
Option 4 has been selected
>>

Cette question est clôturée.

Community Treasure Hunt

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

Start Hunting!

Translated by