Legend - for plots in if condition

5 views (last 30 days)
Meera
Meera on 30 Jun 2015
Commented: Jan on 4 Jul 2015
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

Answers (1)

Jan
Jan on 30 Jun 2015
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 Comments
Meera
Meera on 30 Jun 2015
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 on 4 Jul 2015
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!