How to plot a 2D graph using for loop values?

3 vues (au cours des 30 derniers jours)
sha
sha le 3 Juin 2015
Commenté : Image Analyst le 20 Fév 2018
Hello, Could anyone tell me what the problem is in the following code?
Many thanks.
% code:
lambda= [1.5:0.005:1.6];
llambda=length(lambda);
for di=1:llambda
k(di)=2*3.14/lambda(di)
end
lambda= [1.5:0.005:1.6];
plot(lambda,k);
  4 commentaires
Joseph Cheng
Joseph Cheng le 3 Juin 2015
I am trying. The main question is why is the plot in your sample code not what you're trying to get. If it is then what is the question or is the question to plot within the for loop?
sha
sha le 3 Juin 2015
Modifié(e) : sha le 3 Juin 2015
If I run the code, the following errors appear:
Error using plot Vectors must be the same lengths.
Error in Untitled7 (line 7) plot(lambda,k);
How to fix this error?

Connectez-vous pour commenter.

Réponse acceptée

sha
sha le 3 Juin 2015
Thanks, Cheng. I would like to plot the same thing, however. using for loop. Just curious how to plot the data using for loop? Could you help?

Plus de réponses (2)

Image Analyst
Image Analyst le 3 Juin 2015
The code works fine:
lambda= [1.5:0.005:1.6];
llambda = length(lambda);
for di = 1 : llambda
k(di) = 2 * 3.14 / lambda(di)
end
plot(lambda,k);
It plots with absolutely no warnings or errors about different lengths. What is the actual code you ran (it's not that , that's for sure)?
  1 commentaire
sha
sha le 3 Juin 2015
Thanks, It works now. :)

Connectez-vous pour commenter.


jyoti mishra
jyoti mishra le 20 Fév 2018
how to plot a vector and its delayed version in matlab?
  1 commentaire
Image Analyst
Image Analyst le 20 Fév 2018
Try using the plot() function.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Graphics Performance 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