How to use loops to plot add graphs to a plot depending on checkbox selection
2 ビュー (過去 30 日間)
古いコメントを表示
I have created a checkbox with 5 different selections and I can't figure out how to use a loop to plot a graph containing the data that's been selected below is the code i've got so far
fig = figure;
checkbox_USD = uicontrol(fig,'Style','Checkbox','String','USD','Position',[10 105 100 15],...
'Callback', @Selected_USD);
checkbox_EUR = uicontrol(fig,'Style','Checkbox','String','EUR','Position',[10 85 100 15],...
'Callback', @Selected_EUR);
checkbox_AUD = uicontrol(fig,'Style','Checkbox','String','AUD','Position',[10 65 100 15],...
'Callback', @Selected_AUD);
checkbox_CAD = uicontrol(fig,'Style','Checkbox','String','CAD','Position',[10 45 100 15],...
'Callback', @Selected_CAD);
checkbox_HKD = uicontrol(fig,'Style','Checkbox','String','HKD','Position',[10 25 100 15],...
'Callback', @Selected_HKD);
function Selected_USD (h,eventdata)
plot_USD = get(h,'Value');
disp(plot_USD);
end
function Selected_EUR (h,eventdata)
plot_EUR = get(h,'Value');
disp(plot_EUR);
end
function Selected_AUD (h,eventdata)
plot_AUD = get(h,'Value');
disp(plot_AUD);
end
function Selected_CAD (h,eventdata)
plot_CAD = get(h,'Value');
disp(plot_CAD);
end
function Selected_HKD (h,eventdata)
plot_HKD = get(h,'Value');
disp(plot_HKD);
end
i have already created 5 different functions that plot each graph the code for each is more or less identical
function graph_function_AUD
AUD = readtable('exchange_ratesAUD.xlsx');
AUD.Date = datenum(AUD{:,1});
AUD_array = table2array(AUD);
plot(AUD_array(:,1),AUD_array(:,3));
title('GBP/AUD Exchange Rate');
xlabel('Date');
ylabel('AUD');
yearly_tick =datenum(1999:1:2019,1,1);
set(gca,'xtick',yearly_tick);
datetick('x','mmm/yyyy','keepticks');
xtickangle(90);
set(gca,'XMinorTick','on');
end
any help would hbe much appreciated :))
0 件のコメント
回答 (3 件)
Stijn Haenen
2019 年 12 月 7 日
You can get the value of the checkbox number i with
v=fig.Children(i).Value
So maybe you can make a for loop like this:
figure()
hold on
for i=1:5
v=fig.Children(i).Value
if v==1
plot ...
end
end
(note that is your script checkbox HKD is child number 1 and USD is child number 5)
2 件のコメント
Stijn Haenen
2019 年 12 月 8 日
I think you can use this script as an example:
close all
fig = figure;
checkbox_USD = uicontrol(fig,'Style','Checkbox','String','USD','Position',[10 105 50 15],...
'Callback', @Selected_USD);
checkbox_EUR = uicontrol(fig,'Style','Checkbox','String','EUR','Position',[10 85 50 15],...
'Callback', @Selected_EUR);
checkbox_AUD = uicontrol(fig,'Style','Checkbox','String','AUD','Position',[10 65 50 15],...
'Callback', @Selected_AUD);
checkbox_CAD = uicontrol(fig,'Style','Checkbox','String','CAD','Position',[10 45 50 15],...
'Callback', @Selected_CAD);
checkbox_HKD = uicontrol(fig,'Style','Checkbox','String','HKD','Position',[10 25 50 15],...
'Callback', @Selected_HKD);
axes1 = axes('Parent',fig,...
'Position',[0.184642857142857 0.11 0.720357142857143 0.815]);
box on
axis([0 10 0 50])
hold on
for i=1:5
plot((1:10)*i)
end
while ishandle(1)
for i=1:5
v=fig.Children(i).Value;
if v==1
fig.Children(6).Children(i).Visible='on';
elseif v==0
fig.Children(6).Children(i).Visible='off';
end
pause(0.01)
end
end
function Selected_USD (h,eventdata)
plot_USD = get(h,'Value');
disp(plot_USD);
end
function Selected_EUR (h,eventdata)
plot_EUR = get(h,'Value');
disp(plot_EUR);
end
function Selected_AUD (h,eventdata)
plot_AUD = get(h,'Value');
disp(plot_AUD);
end
function Selected_CAD (h,eventdata)
plot_CAD = get(h,'Value');
disp(plot_CAD);
end
function Selected_HKD (h,eventdata)
plot_HKD = get(h,'Value');
disp(plot_HKD);
end
Stijn Haenen
2019 年 12 月 8 日
Which matlab version do you have? I have 2019a and it works.
What do you see if you typ in the command window:
fig.Children
and
fig.Children(6).Children
2 件のコメント
参考
カテゴリ
Help Center および File Exchange で Interactive Control and Callbacks についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!