How to stop legend adding data1 to figure when adding data from if statement

4 次查看(过去 30 天)
Hi, I have a dialogue box that asks the user what is needed to be plotted. I have 9 data sets and these successfully plot one/multiple data sets depending on the user selection. However if multiple data sets are selected, the first is correctly named in the legend but the remaining data is named 'data1', 'data2' etc.
How can the data be named after the correct name at that point in the if statement?
Thanks
plot_choice = {'Digitised Gaussean Beam', 'Digitised 2 Minute Shots', 'Interpolated Gaussean Beam Values', 'Interpolated 2 Minute Shot Values', 'Uncorrected Experiment Gaussean Beam', 'Uncorrected Experiment 2 Minute Shots', 'Corrected Experiment Gaussean Beam', 'Corrected Experiment 2 Minute Shots', 'Corrected Theoretical Gaussean Beam'};
plot_choice_text = {'Select Values To Be Plotted:-'};
[plot_choice_index,plot_choice_tf] = listdlg('PromptString', plot_choice_text, 'ListSize', [250 125], 'ListString', plot_choice, 'Name', 'Plot Selection');
if plot_choice_tf == 0
warndlg('No Plot Selected', 'Warning!')
return
end
a1 = ismember(1, plot_choice_index);
a2 = ismember(2, plot_choice_index);
a3 = ismember(3, plot_choice_index);
a4 = ismember(4, plot_choice_index);
a5 = ismember(5, plot_choice_index);
a6 = ismember(6, plot_choice_index);
a7 = ismember(7, plot_choice_index);
a8 = ismember(8, plot_choice_index);
a9 = ismember(9, plot_choice_index);
figure(1)
hold on
if a1 == 1
plot(x, y_gaussean_beam, 'r-o');
legend('Digitised Gaussean Beam');
legend('Location', 'southwest', 'AutoUpdate', 'off');
end
if a2 == 1
plot(x, y_2_minute_shots, 'b-o');
legend('Digitised 2 Minute Shots');
legend('Location', 'southwest', 'AutoUpdate', 'off');
end
if a3 == 1
plot(interp_x_array, interpolation_gaussean_beam, 'r.');
legend('Interpolated Gaussean Beam');
legend('Location', 'southwest', 'AutoUpdate', 'off');
end
if a4 == 1
plot(interp_x_array, interpolation_2_minute_shots, 'b.');
legend('Interpolated 2 Minute Shots');
legend('Location', 'southwest', 'AutoUpdate', 'off');
end
if a5 == 1
plot(our_delta, our_gaussean_beam, 'r*');
legend('Uncorrected Exp Gaussean Beam');
legend('Location', 'southwest', 'AutoUpdate', 'off');
end
if a6 == 1
plot(our_delta, our_2_minute_shots, 'b*');
legend('Uncorrected Exp 2 Minute Shots');
legend('Location', 'southwest', 'AutoUpdate', 'off');
end
if a7 == 1
plot(our_delta, our_gaussean_beam_corrected, 'r-*');
legend('Corrected Exp Gaussean Beam');
legend('Location', 'southwest', 'AutoUpdate', 'off');
end
if a8 == 1
plot(our_delta, our_2_minute_shots_corrected, 'b-*');
legend('Corrected Exp 2 Minute Shots');
legend('Location', 'southwest', 'AutoUpdate', 'off');
end
if a9 == 1
plot(our_delta, our_fwhm, 'g-*');
legend('Theoretical Gaussean Beam');
legend('Location', 'southwest', 'AutoUpdate', 'off');
end
xlabel('Focal Position (\mum)');
ylabel('FWHM (\mum)');
title('Digitised Defocus vs FWHM');
x_axis_ticks = linspace(-120, 120, 13); %xaxis ticks between -120 to 120 spaced 20um apart
axis([-135 80 0 27]); %axes size [x1 x2 y1 y2]
xticks(x_axis_ticks);
ax = gca;
ax.YAxisLocation = 'origin'; %Centre y axis on the origin
hold off
  1 个评论
Jan
Jan 2018-5-11
The "if true" shows, that you have pressed the "{} Code" button already, but in the wrong order. Paste the code at first, than mark it with the mouse and then press this button. Afterwards your code is readable. Please explain, what "the correct name" is. This is obvious for you, but unknown for the readers.

请先登录,再进行评论。

采纳的回答

Jan
Jan 2018-5-11
A general hint at first:
match = ismember(1:9, plot_choice_index);
Not replace if a4 == 1 by if match(4). Even better, you can do this in a loop. You can defined the plot styles and titles as cell string:
PlotStyle = {'r-o', 'b-o', ...}
Data = {y_gaussean_beam, y_2_minute_shots, ...}
Name = plot_choice;
for k = 1:length(match)
if match(k)
plot(x, Data{k}, PlotStyle{k});
legend(Name{k});
...
end
end
I assume, that the creation of the Data array is not needed, because I guess, that you have obtained the single variables from one matrix before.
The rule is to avoid repeated code but use loops when possible. This simplifies the code and increases the flexibility. If now a solution to your question is found, you have to change the code in one place only, not in all branches.
I assume, a solution is to move the legend command to the bottom:
for k = 1:length(match)
if match(k)
plot(x, Data{k}, PlotStyle{k});
...
end
end
legend(Name(match));

更多回答(1 个)

D F
D F 2018-5-15
That's great - thanks!

类别

Help CenterFile Exchange 中查找有关 Line Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by