Info

此问题已关闭。 请重新打开它进行编辑或回答。

"Activate" lines with a GUI?

1 次查看(过去 30 天)
mec123
mec123 2016-7-27
关闭: MATLAB Answer Bot 2021-8-20
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 个评论
Stefan Reich
Stefan Reich 2016-7-28
How about an if/else in the pushbotton_Callback function?
mec123
mec123 2016-7-28
Thank you! An if/else in the pushbutton reffering to the results of the if/else's of the checkboxes.

回答(1 个)

Stephen23
Stephen23 2016-7-28
编辑:Stephen23 2016-7-29
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
>>

此问题已关闭。

Community Treasure Hunt

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

Start Hunting!

Translated by