How to use loops to plot add graphs to a plot depending on checkbox selection

조회 수: 1 (최근 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 :))

답변 (3개)

Stijn Haenen
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
Nathanael Durham
Nathanael Durham 2019년 12월 7일
any idea how i can remove the plot when the box is deselected?
Nathanael Durham
Nathanael Durham 2019년 12월 7일
So far i've got some code that succcesfully adds the data to the same plot however when the box is deselected, the plot stays on the graph
fig = figure; %creates checkbox with the currency selections
checkbox_USD = uicontrol(fig,'Style','Checkbox','String','USD','Position',[10 105 45 15],...
'Callback', @Selected_USD); %callback allows a function to operate when box is selected
checkbox_EUR = uicontrol(fig,'Style','Checkbox','String','EUR','Position',[10 85 45 15],...
'Callback', @Selected_EUR);
checkbox_AUD = uicontrol(fig,'Style','Checkbox','String','AUD','Position',[10 65 45 15],...
'Callback', @Selected_AUD);
checkbox_CAD = uicontrol(fig,'Style','Checkbox','String','CAD','Position',[10 45 45 15],...
'Callback', @Selected_CAD);
checkbox_HKD = uicontrol(fig,'Style','Checkbox','String','HKD','Position',[10 25 45 15],...
'Callback', @Selected_HKD);
function Selected_USD (h,~)%callback for 'USD' selection
checked_USD = get(h,'Value');
if checked_USD == 1
graph_function_USD
hold on
end
end
function Selected_EUR (h,~) %callback for 'EUR' selection
checked_EUR = get(h,'Value');
if checked_EUR == 1
graph_function_EUR
hold on
end
end
function Selected_AUD (h,~) %callback for 'AUD' selection
checked_AUD = get(h,'Value');
if checked_AUD == 1
graph_function_AUD
hold on
end
end
function Selected_CAD (h,~) %callback for 'CAD' selection
checked_CAD = get(h,'Value');
if checked_CAD == 1
graph_function_CAD
hold on
end
end
function Selected_HKD (h,~) %callback for 'HKD' selection
checked_HKD = get(h,'Value');
if checked_HKD == 1
graph_function_HKD
hold on
end
end
also the graph_function_USD is a seperate function i've made which produces the plot i'll show it below
function graph_function_USD
USD = readtable('exchange_ratesUSD.xlsx'); %loads excel file for USD exchange rates
USD.Date = datenum(USD{:,1}); %converts dates from string to serial date number
USD_array = table2array(USD); %converts table to matrix
plot(USD_array(:,1),USD_array(:,3)); %plots date against exchange rate
title('GBP/USD Exchange Rate');
xlabel('Date');
ylabel('Comparative Exchange Rate');
yearly_tick =datenum(1999:1:2019,1,1); %set ticks to occur in yearly intervals
set(gca,'xtick',yearly_tick);
datetick('x','mmm/yyyy','keepticks');
xtickangle(90); %rotate x-axis labels to prevent cluttering
set(gca,'XMinorTick','on');
legend('USD')
end

댓글을 달려면 로그인하십시오.


Stijn Haenen
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
  댓글 수: 1
Nathanael Durham
Nathanael Durham 2019년 12월 8일
When i try using this i get the following error,
Unrecognized property 'Visible' for class 'matlab.graphics.GraphicsPlaceholder'.
Error in checkboxplottest (line 28)
fig.Children(6).Children(i).Visible = 'on';

댓글을 달려면 로그인하십시오.


Stijn Haenen
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

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by