error named as "Subscript indices must either be real positive integers or logicals".
3 views (last 30 days)
Show older comments
The error is observed in the following equation
s(t)= Ac*cos((2*pi*fc*t)+(B*sin(2*pi*fm*t)));
0 Comments
Answers (1)
Walter Roberson
on 3 Mar 2016
1) Possibly you have created a variable named "sin" or "cos". In that case, the sin() would be understood as a subscript indexing request rather than as a call to the function sin()
2) Possibly your t is not a positive integer. In that case, the s(t) on the left side would be understood as a subscript indexing request at a value that is not a positive integer.
It is common for people to write try to write in "formula" notation, that they want to define "s" as a formula that, given t, returns Ac*cos((2*pi*fc*t)+(B*sin(2*pi*fm*t))) . In MATLAB, the way to do that would be
s = @(t) Ac*cos((2*pi*fc*t)+(B*sin(2*pi*fm*t)));
This notation is not for any particular value of t, it is a formula. An example of its use would be
x = 0 : 0.1 : 10;
y = s(x);
This would invoke the formula stored in s, passing in the value stored in the vector x, with that value temporarily called "t" within the formula, with the result of acting on those particular values being computed and returned.
It is also common for people to try to write in formula notation even when they are assigning a value within a loop, such as
for t = 0 : 0.1 : 10
s(t)= Ac*cos((2*pi*fc*t)+(B*sin(2*pi*fm*t)));
end
intending to process a particular t value in each iteration and associate the result with "t" in the vector "s". You can see, though, that in order for that to work, the vector would have to be indexed by a floating point number, and that is something that MATLAB just doesn't support.
What would work:
tvals = 0 : 0.1 : 10;
for tidx = 1 : length(tvals)
t = tvals(tidx);
s(tidx) = Ac*cos((2*pi*fc*t)+(B*sin(2*pi*fm*t)));
end
2 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!