Effacer les filtres
Effacer les filtres

If elseif else end function

29 vues (au cours des 30 derniers jours)
son
son le 23 Août 2014
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?

Réponses (2)

Matt J
Matt J le 23 Août 2014
Modifié(e) : Matt J le 23 Août 2014
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 commentaires
Matt J
Matt J le 23 Août 2014
Modifié(e) : Matt J le 23 Août 2014
Or,
N=length(coeff);
args(2,1:N)=num2cell(L.'*coeff,1);
args(1,:)={L};
plot(args{:});
son
son le 23 Août 2014
i mean the first 3 case above will produce 1 plot . and then the final case will produce 3 line in 1 graph.

Connectez-vous pour commenter.


Image Analyst
Image Analyst le 23 Août 2014
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;

Catégories

En savoir plus sur Printing and Saving dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by