index must be a positive integer or logical.
조회 수: 25 (최근 30일)
이전 댓글 표시
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
Please help me to solve dis.....
댓글 수: 0
채택된 답변
Junaid
2011년 12월 29일
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.
댓글 수: 0
추가 답변 (5개)
Walter Roberson
2011년 12월 29일
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);
댓글 수: 0
Junaid
2011년 12월 29일
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])
댓글 수: 0
Matt Tearle
2011년 12월 29일
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.)
댓글 수: 0
dark
2011년 12월 29일
편집: Walter Roberson
2016년 8월 21일
댓글 수: 1
Walter Roberson
2011년 12월 30일
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
2012년 10월 15일
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
댓글 수: 1
Walter Roberson
2016년 8월 21일
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);
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!