Legend - for plots in if condition

4 次查看(过去 30 天)
Meera
Meera 2015-6-30
评论: Jan 2015-7-4
for j = 1:laenge4(1,2)
if isempty(TempVolt(j).data) == true;
else
x_axes = volt_data(:,1);
y_axes = time_data(:,2);
if volt_data(1)== 7
a1 = plot(x_axes,y_axes,'bs','DisplayName','@7V');
hold on;
end
if volt_data(1)== 13.5
a2 = plot(x_axes,y_axes,'gs','DisplayName','@13.5V');
hold on;
end
if volt_data(1)== 18
a3 = plot(x_axes,y_axes,'ks','DisplayName','@18V');
hold on;
end
end
end
l = legend('show')
set(l, 'Interpreter','none');
But I want to display all 3 legends even if the plots a2 and a3 are not plotted. Can anyone please help me. Becoz in for loop for other values of 'i' a2 or a3 can be plotted.
Please help me

回答(1 个)

Jan
Jan 2015-6-30
What about setting the visibility?
for j = 1:laenge4(1,2)
if ~isempty(TempVolt(j).data)
x_axes = volt_data(:,1);
y_axes = time_data(:,2);
a1 = plot(x_axes,y_axes,'bs','DisplayName','@7V', ...
'Visible', OnOffStr(volt_data(1) == 7));
hold on;
a2 = plot(x_axes,y_axes,'gs','DisplayName','@13.5V', ...
'Visible', OnOffStr(volt_data(1)== 13.5));
a3 = plot(x_axes,y_axes,'ks','DisplayName','@18V', ...
'Visible', OnOffStr(volt_data(1)== 18));
end
end
function S = OnOffStr(V)
if V
S = 'on';
else
S = 'off';
end
Does this works or do invisible line vanish from the legend?
  2 个评论
Meera
Meera 2015-6-30
Hello Jan Simon,
I got the following error 'Function definition is misplaced or improperly nested.' can we use the statement 'function' in the middle of the program.?
Thanking you. Meera
Jan
Jan 2015-7-4
Function definitions are allowed inside function M-files, not in scripts and not in the command window. So either create a new file called OnOffStr.m and store it in a user-defined folder inside your Matlab path. Or append teh definition of the function to your function file. If you use a script currently, think of converting it to a function.

请先登录,再进行评论。

类别

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

标签

Community Treasure Hunt

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

Start Hunting!

Translated by