"what's wrong with my code?"

- you preallocate a variable named Vho which you never use again.
- inside the loop you allocate to a variable named Vso , but you do not use any indexing, so on each loop iteration you simply overwrite Vso, thus leaving only the (scalar value) from the final iteration.

So even though you write that you are plotting a vector, if you actually take a look at Vso it is actually scalar (and plotting a scalar gives the appearance of an empty plot, although it isn't actually, just the default linespec does not include any point marker).

I fixed those mistakes (and replaced heaviside with a simple logical comparison):

Vsw = zeros(1,N);

for k = 1:N

Vsw(k) = -V0*((x(k)+a/2)>0 - (x(k)-a/2)>0);

end

plot(Vsw,'-*')

## 1 Comment

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/531768-how-to-plot-a-vector#comment_860033

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/531768-how-to-plot-a-vector#comment_860033

Sign in to comment.