Effacer les filtres
Effacer les filtres

very strange error of for loop

2 vues (au cours des 30 derniers jours)
NoYeah
NoYeah le 16 Avr 2020
Commenté : Star Strider le 16 Avr 2020
I made this kind structure code
but the result is not I had expecetd
k=1;
for i=1:1:100
if(i==1)
a=zeros(size(i));
b=zeros(size(i));
end
a(k)=sin(i);
b(k)=a(k)
k=k+1
end
a(k) display the sin wave
but b(k) dispay only one constant value
like y=3
what happend to my code?

Réponse acceptée

Star Strider
Star Strider le 16 Avr 2020
You did not include the plot call you are using.
If you are plotting:
figure
plot(a,b)
it will show a straight line from (-1,-1) to (1,1) because both ‘a’ and ‘b’ have the same values.
.
  2 commentaires
NoYeah
NoYeah le 16 Avr 2020
yes it has to be same
but my code they are not the same
werid
Star Strider
Star Strider le 16 Avr 2020
I am using R2020a, and Windows 10.
Is that the only code you are running in that script?
There could be version differences, however I doubt they would be that extreme.
If you run this line:
which plot -all
from your Command Window or a script, all the results should share this part of the path to each (the exact configuration could be operating-system dependent):
C:\Program Files\MATLAB\R2020a\toolbox\matlab\
The rest oif the path will likely vary. If any do not, and it turns out that you have another function named ‘plot’, that could be the problem. (Naming user-defined functions the same as MATLAB built-in functions is called ‘overshadowing’ and is to be avoided.)
Beyond that, I have no idea what the problem could be.
.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Interactive Control and Callbacks dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by