Plotting on GUI and navigate between the figures
Show older comments
Hello everyone,
So, I want to plot some graphs in my GUI, but using the same axes object. And then, I'll have two buttons to navigate between the figures. Something like what is shown in the fig below.

Any ideas on how to do this?
I tried creating a sequence with all the graphs that will be created, and assign a signal to the two buttons of the fig. above (-1 and 1).
function manageGraphs(handles, hObject, sinal)
% Define the sequences of graphs, for each topology
sequence = {'perdas_ripple', 'volume_ripple', ...
'perdas_fsw','volume_fsw', ...
'pareto_ripple', 'pareto_fsw'};
dim_sequence = length(sequence);
start_graphs = 1;
end_graphs = 6;
% Get the current index_sequence from handles
index_sequence = 4;
fsw = handles.fsw;
delta_i = handles.delta_i;
if (sinal == -1)
% If the user pressed the left button, i.e., sinal = -1
if(index_sequence ~= start_graphs)
% If the user did not reach the first element
% index_sequence ~= start_current_graphs
% Sweep to the previous graph, if the user did not reach the
% beginning of the list
index_sequence = index_sequence + sinal;
else
% index_sequence == start_losses_graphs
% If the user reaches the start, returns to the end
index_sequence = end_graphs;
end
% If the sequence ends, return to the start
elseif sinal == 1
% If the user pressed the right button, i.e., sinal = 1
if(index_sequence ~= end_graphs)
index_sequence = index_sequence + sinal;
else
% If the user reached the last element in the sequence of graphs
% of losses, returns to the start of this list
index_sequence = start_graphs;
end
end
seq = sequence{index_sequence};
% Make 'grafico_corrente' the current graph
axes(handles.graphs);
% Plot the graphs
switch seq
case 'perdas_ripple'
plot(handles.x_ripple, handles.PerdasT_ripple); hold on;
plot(handles.x_ripple, handles.PerdasL_ripple); hold on;
plot(handles.x_ripple, handles.PerdasTotais_ripple);
xlabel('Ripple de Corrente');
ylabel('Perdas [W]');
title('Perdas nos Transistores, Indutores e Totais');
legend('Perdas nos Transistores', 'Perdas nos Indutores', 'Perdas Totais');
case 'volume_ripple'
plot(handles.x_ripple, handles.VolL_ripple); hold on;
plot(handles.x_ripple, handles.VolHS_ripple); hold on;
plot(handles.x_ripple, handles.VolumeTotal_ripple);
xlabel('Ripple de Corrente');
ylabel('Volume [dm³]'); % Verificar se é dm³ msm
title('Volume dos Indutores, Dissipadores e Total');
legend('Volume dos Indutores', 'Volume dos Dissipadores', 'Volume Total');
case 'perdas_fsw'
plot(handles.x_fsw, handles.PerdasT_fsw); hold on;
plot(handles.x_fsw, handles.PerdasL_fsw); hold on;
plot(handles.x_fsw, handles.PerdasTotais_fsw);
xlabel('Frequência de Chaveamento');
ylabel('Perdas [W]');
title('Perdas nos Transistores, Indutores e Totais');
legend('Perdas nos Transistores', 'Perdas nos Indutores', 'Perdas Totais');
case 'volume_fsw'
plot(handles.x_fsw, handles.VolL_fsw); hold on;
plot(handles.x_fsw, handles.VolHS_fsw); hold on;
plot(handles.x_fsw, handles.VolumeTotal_fsw);
xlabel('Frequência de Chaveamento');
ylabel('Volume [dm³]'); % Verificar se é dm³ msm
title('Volume dos Indutores, Dissipadores e Total');
legend('Volume dos Indutores', 'Volume dos Dissipadores', 'Volume Total');
case 'pareto_ripple'
for k = 1 : length(delta_i)
plot(handles.DenPot(k,1:end), handles.Rend(k,1:end)); hold on;
legend("Ripple = " + delta_i(k));
end
xlabel('Densidade de Potência [kW/dm³]');
ylabel('Rendimento');
title('Rendimento x Densidade de Potência - Diferentes Ripples');
case 'pareto_fsw'
for k = 1 : length(fsw)
plot(handles.DenPot(:,k), handles.Rend(:,k)); hold on;
legend("Fsw = " + fsw(k) + "kHz");
end
xlabel('Densidade de Potência [kW/dm³]');
ylabel('Rendimento');
title('Rendimento x Densidade de Potência - Diferentes Fsw');
end
end
obs.: Is it correct how I made the plot loops and the legends of the two last cases (pareto_ripple and pareto_fsw) ?
Thanks in advance!
7 Comments
Adam Danz
on 14 Jan 2020
The approach you seem to be taking is to toggle through a list of data sets and destroy & produce a plot on each button click. Each button click pulls the data from your GUI and produces a new figure within the existing axes.
What if, instead, you had all 6 axes on top of eachother and in each axes, the data are always plotted and up to date. The axes are updated every time a GUI component is changed. Then, instead of producing the plot when the buttons are pressed, you merely set the current axes visibility to 'off' and the next axes visibility of 'on'.
That proposal will still require some time to produce all of the plots but only one of them will be visible at a time and the toggle action will be much faster.
Pedro Augusto de Castro e Castro
on 15 Jan 2020
Adam Danz
on 15 Jan 2020
"I put all the axes on the GUI and set their visibility to off, initially. "
It might make more sense to leave one of the axes visible; whichever axes is the default, initial axes.
"When I press the button to run the main function, I plot the result on the first axes."
That approach is fine but my recommendation was to produce all of the plots ahead of time so that when you toggle through the axes, the date merely appears rather than being re-drawn every time. That will likely require some restructuring of your code.
"As shown in the code below. The problem is doing it like that all the graphs are plotted, the axis become a mess as does the title."
I haven't looked too deeply into your code but it appears that you're not using axis handles as inputs to the plot(), title() etc. It's generally good practice to always specify the axis handle.
plot(axisHandle, x, y);
title(axisHandle, 'myTitle')
Note that this problem will alos be solved by producing the plots ahead of time. The point of my suggestion is that toggling through the axes merely makes them visible/invisible and there is no need to do any plotting when toggling through the axes.
"Other thing, how do I know which one is the active axes the moments I press the 'go back' or 'go forward' button? "
Presumably you have a list of axes such as
handles.ax1
handles.ax2
handles.ax3
To see which one is visible, look at their Visible property or you could keep track of how many times the << and >> buttons were pressed.
Pedro Augusto de Castro e Castro
on 15 Jan 2020
Adam Danz
on 15 Jan 2020
Ahhh.... sorry. I forgot that axis off only turns off the axes and does not turn off the content of the axes.
The small fix is to apply the visibility toggle to axis children, too.
set(handles.graph_1, 'Visible', 'off');
set(handles.graph_1.Children, 'Visible', 'off');
Pedro Augusto de Castro e Castro
on 15 Jan 2020
Adam Danz
on 15 Jan 2020
Every time you want to turn an axes on or off you must execute both lines of code.
set(handles.graph_1, 'Visible', 'off'); % or 'on'
set(handles.graph_1.Children, 'Visible', 'off'); % or 'on'
Accepted Answer
More Answers (0)
Categories
Find more on Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!