필터 지우기
필터 지우기

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