Borrar filtros
Borrar filtros

How to remove data from a plot when box in checkbox is deselected

5 visualizaciones (últimos 30 días)
I have created a figure with 5 checkboxes and have created functions such that when each box is checked, the respective data is added to the plot
fig = figure; %creates checkbox with the currency selections
checkbox_USD = uicontrol(fig,'Style','Checkbox','String','USD','Position',[10 105 100 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 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,~)%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
my question is as follows,
how do i make it so that when the box is deselected, the data is removed from the plot?
any help would be appreciated :)

Respuesta aceptada

Max Murphy
Max Murphy el 8 de Dic. de 2019
Editada: Max Murphy el 8 de Dic. de 2019
Edit: After getting the graph_function, removing the old suggestions for clarity.
% These two lines are most likely to change, put them at the top
currency_type = {'USD','EUR','AUD','CAD','HKD'}
source_file = {'exchange_ratesUSD.xlsx','each other name here'} % has to be completed
fig = figure('Name','Currency Data by Type');
ax = axes(fig);
% fig_height = fig.Position(4); see 'Position' of checkbox_obj
line_obj = gobjects(1,numel(currency_type));
checkbox_obj = gobjects(1,numel(currency_type));
for i = 1:numel(line_obj)
line_obj(i) = line(ax,nan,nan); % Where i == 1 corresponds to USD, etc. etc.
set(line_obj(i),'UserData',struct('type',currency_type{i},'source',source_file{i})); % Associate type with the line object
checkbox_obj(i) = uicontrol(fig,...
'Style','Checkbox',...
'String',currency_type{i},...
'Position',[10 105-20*i 100 15],... % could use fig_height here instead of 105
'Callback', {@Selected_Callback,line_obj(i)});
end
% note, want to remove "currency_type" here:
function Selected_Callback (h,~,line_obj)
checked = get(h,'Value');
graph_function(checked,line_obj);
end
And for the function referenced by the callback function:
function graph_function(checked,line_obj)
% GRAPH_FUNCTION Graph data depending if checkbox is ticked and which checkbox it is
currency_type = line_obj.UserData.type; % Retrieve the thing we associated with l(i)
if ~checked
set(line_obj,'XData',nan,'YData',nan); % "turn off the line"
% note that this can be done using set(line_obj,'Visible','off');
% however, if you have a lot of data points, then it can be faster just to do it this way.
else
data = readtable(line_obj.UserData.source);
x = table2array(data(:,1));
y = table2array(data(:,3));
set(line_obj,'XData',x,'YData',y);
end
end
  11 comentarios
Max Murphy
Max Murphy el 8 de Dic. de 2019
I'd suggest naming the script something else, such as currencyType_checkboxTest or something that fits your name convention.
Yes, the cell arrays currency_type and source_file in the code from the top-level comment would need to be manually defined by you. Each element of source_file should match the corresponding element of currency_type.
source_file should be the names of the xlsx source files. You can also parse their names dynamically if you have them all in one folder by calling the dir command on that folder, but I think you just want to get the test example working so I wouldn't worry about that for now.
Nathanael Durham
Nathanael Durham el 8 de Dic. de 2019
What should the currency array look like?

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Interactive Control and Callbacks en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by