# index must be a positive integer or logical.

30 views (last 30 days)
dark on 29 Dec 2011
Commented: Walter Roberson on 21 Aug 2016
hi, I'm trying to solve a half wave rectifier problem. But i'm getting an error as
Attempted to access v(1.1); index must be a positive integer or logical.
Error in ==> Untitled at 4
v(t)=0.1*sin(t)
Also i'm getting single discrete value in the plot
Here is my code
clear all; close all;
for t=1:0.1:2*pi
if t<pi
v(t)=0.1*sin(t)
else
v(t)=0;
end
stem(t,v);
end

Junaid on 29 Dec 2011
Dear one possible way is this.
clear all; close all;
figure;
hold on;
index = 1;
for t=1:0.1:2*pi
if t<pi
v(index)=0.1*sin(t);
else
v(index)=0;
end
stem(t,v(index));
index = index + 1;
end
I just have removed your syntax error with best of my understanding. As you want to plot stem for each value of t. I m not sure either you get your desired result or not. Check if it works.

Walter Roberson on 29 Dec 2011
Slightly different style than Junaid's answer, and with the plotting corrected.
timelist = 1:0.1:2*pi;
v = zeros(size(timelist));
for t=1:length(timelist)
if timelist(t)<pi
v(t)=0.1*sin(timelist(t))
else
v(t)=0;
end
end
stem(timelist,v);

Junaid on 29 Dec 2011
Dear Dark.
In your previous example you have vector v. and to access each element in any vector you have to give positive integer value, you can't give continuous or values <= 0. and in the graph to set values to continus just set the xtick to your deseried values. for example
set(gca,'XTick',[1:.5:7])

Matt Tearle on 29 Dec 2011
A stem plot is for discrete time points, by its very nature. Indeed, all plots are discrete at some level. Your loop is in time steps of 0.1, so your plot is discretized to that level by necessity. So I'm not sure what you mean by your question about getting a continuous signal.
As for the error you were getting, MATLAB interprets v(t) as an indexing into the array v. Specifically, you're asking for the t-th element of v. That means MATLAB expects a positive integer for t. v(3) = sin... means "set the third element of v to be [whatever sin... turns out to be]". But you were looping over values of t in steps of 0.1, so v(t) was things like v(1.1), v(1.2), etc. You can't access the 1.1th element or 1.2th element of v.
Junaid's solution uses an integer counter, index. Walter's solution loops over the variable t which takes integer values.
There is a way to index based on a condition, rather than an index number. It's called logical indexing and would actually enable you to avoid the loop altogether:
t = 1:0.01:2*pi; % make a vector of t values
y = 0.1*sin(t); % calculate corresponding y values
y(t >= pi) = 0; % set y to 0 whenever t >= pi
stem(t,y) % or plot(t,y)
The third line uses a logical index to extract the values of y under a given condition -- t >= pi -- and changes them to be 0.
Finally, you might want to use the linspace function, rather than t = 1:0.1:2*pi, if you want a value of t at 2*pi. Using the colon means that the last value of t will be the nearest multiple of 0.1 no greater than 2*pi. (Because 2*pi is irrational, the last value will therefore be less than 2*pi.)

dark on 29 Dec 2011
Edited: Walter Roberson on 21 Aug 2016
Thanks for ur reply.. but in d plot i'm getting discrete values.. How to get continious signal?? Also why dat previous error was occuring???
Walter Roberson on 30 Dec 2011
You cannot get a continuous signal in any MATLAB plot. A continuous signal requires an infinite amount of data.
If what you want instead is for the points to be joined by a line, then instead of stem(t,y), use plot(t,y)

Peyman on 15 Oct 2012
Hi,
I am receiving same error! My data starts from 0 and the plotting should start from 0 too. I used like this changed you did, (index,...) and the plot started from 1, not from 0. what should I do to start plotting and calculating from zero and not having this error?
for t=[0:190];
T_h(t)=deltaT_or*(((1+R*(K^2))/(1+R))^x)-deltaT_oi)*(1-exp((-t)/(k11*To));
plot(t,T_h)
end
Walter Roberson on 21 Aug 2016
timelist = 0:190;
T_h = zeros(size(timelist));
for tidx=1:length(timelist)
t = timelist(tidx);
T_h(tidx) = deltaT_or*(((1+R*(K^2))/(1+R))^x)-deltaT_oi)*(1-exp((-t)/(k11*To));
end
plot(timelist, T_h);