If elseif else end function

조회 수: 76 (최근 30일)
son
son 2014년 8월 23일
답변: Image Analyst 2014년 8월 23일
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?

답변 (2개)

Matt J
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
  댓글 수: 2
Matt J
Matt J 2014년 8월 23일
편집: Matt J 2014년 8월 23일
Or,
N=length(coeff);
args(2,1:N)=num2cell(L.'*coeff,1);
args(1,:)={L};
plot(args{:});
son
son 2014년 8월 23일
i mean the first 3 case above will produce 1 plot . and then the final case will produce 3 line in 1 graph.

댓글을 달려면 로그인하십시오.


Image Analyst
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;

카테고리

Help CenterFile Exchange에서 Printing and Saving에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by