very strange error of for loop

1 view (last 30 days)
NoYeah
NoYeah on 16 Apr 2020
Commented: Star Strider on 16 Apr 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?

Accepted Answer

Star Strider
Star Strider on 16 Apr 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 Comments
NoYeah
NoYeah on 16 Apr 2020
yes it has to be same
but my code they are not the same
werid
Star Strider
Star Strider on 16 Apr 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.
.

Sign in to comment.

More Answers (0)

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!