plotting multiple variables in a listbox

Good Morning, I am trying to plot all variables selected in a listbox in a matlab gui. I seem to be overwriting the variables when plotting and only plotting the last one even when using the hold on command inside the loop. I also cannot figure out how to add a legend to the plot which corresponds to the variable selected in the listbox.
Any suggestions?
Thanks so much.
%Setting up string and value for desired parameters
Variable_Name = get(handles.Parameter_Listbox,'String');
Index = get(handles.Parameter_Listbox,'Value');
%Setting results plot
axes(handles.Plot)
%Creating loop for parameters choosen
for i=1:length(Index)
Parameter(:,i)=Data(:,Index(i))
end
[j,k]=size(Parameter);
for p=1:k
plot(Data(:,2),Parameter(:,k));
hold on
legend(Variable_Name(k));
hold on
end

 Respuesta aceptada

Jan
Jan el 18 de Ag. de 2014
You are plotting Data(:, 2) against Data(:, Index). Is this your intention?
What about this:
Variable_Name = get(handles.Parameter_Listbox,'String');
Index = get(handles.Parameter_Listbox,'Value');
plot(Data(:,2), Data(:, index).', 'Parent', handles.Plot);
legend(Variable_Name(index));

4 comentarios

Melissa
Melissa el 18 de Ag. de 2014
AMAZING! thank you!
Melissa
Melissa el 18 de Ag. de 2014
Wait now its saying my legend matrix is empty...
Jan
Jan el 18 de Ag. de 2014
Please post a complete copy of the error message.
Using your same logic I found a way so that the matrix was not empty:
pHandles = plot(Data(:,2), Data(:, Index).', 'Parent', handles.Plot);
legend(pHandles,Variable_Name(Index));
Thanks for all of your help.

Iniciar sesión para comentar.

Más respuestas (1)

Michael Haderlein
Michael Haderlein el 18 de Ag. de 2014
Editada: Michael Haderlein el 18 de Ag. de 2014
You only plot the last Parameter, you need to change the line
plot(Data(:,2),Parameter(:,k));
to
plot(Data(:,2),Parameter(:,p));
The same with the legend, too.

3 comentarios

Melissa
Melissa el 18 de Ag. de 2014
Thanks!! It worked great for the Parameter plotting but not for the legend when changing to p. Any idea why?
Michael Haderlein
Michael Haderlein el 18 de Ag. de 2014
Editada: Michael Haderlein el 18 de Ag. de 2014
Sorry, my comment on the legend was wrong. You overwrite the legend in every iteration. Put the legend command below the loop and write
legend(Variable_Name(Index))
Michael Haderlein
Michael Haderlein el 18 de Ag. de 2014
Anyway, Jan Simon's answer is better as he plots everything without the loop - just follow his answer.

Iniciar sesión para comentar.

Etiquetas

Preguntada:

el 18 de Ag. de 2014

Comentada:

el 18 de Ag. de 2014

Community Treasure Hunt

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

Start Hunting!

Translated by