If elseif else end function
조회 수: 76 (최근 30일)
이전 댓글 표시
hi, i am writing the code that follows
L=1:1:10;
choice = menu('Choose the case','case1','case2','case3','case4');
if choice==1
a = 1;
b=2;
c=3;
elseif choice==2
a = 6;
b=8;
c=9;
else
a = 2;
b=6;
c=8;
end
x=(a/b)^c.*L;
plot(L,x)
but now i want to add case 4 which will plot all three case in one graph? i can do it by calling a1,a2,a3... and so on then plot L1, L2, L3 for 3 case above in 1 graph
but actually my programme is so much bigger than this simple example. Do u have any other suggestion?
댓글 수: 0
답변 (2개)
Matt J
2014년 8월 23일
편집: Matt J
2014년 8월 23일
Do more vectorized processing. The following is easily extendable to more data
a=[1 6 2];
b=[2 8 6];
c=[3,9,8];
coeff=(a./b).^c;
Then you can simply do
for choice=1:length(coeff)
plot(L,coeff(choice).*L); hold on
end
hold off
Image Analyst
2014년 8월 23일
Try this:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 13;
L=1:1:10;
a=[1 6 2];
b=[2 8 6];
c=[3,9,8];
choice = menu('Choose the case','case1','case2','case3','case4')
if choice<= 3
indexes = choice
else
indexes = 1:3
end
x= (a(indexes) ./ b(indexes)) .^ c(indexes) .* L(indexes);
plot(L(indexes), x, 'b*-', 'LineWidth', 2, 'MarkerSize', 15);
grid on;
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Printing and Saving에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!