()-indexing must appear last in an index expression

5 visualizzazioni (ultimi 30 giorni)
I get this error related to this line of code:
A(l,c)=A(l,c)+(b(l)(D(1,h))*b(c)(D(1,h)))
b is a vector that in each position has a function(like x^2), and D is a matrix whose all components are numbers. What i whant to do here is calculate the value of the functon b(l) on D(l,h)

Risposta accettata

Philip Borghesani
Philip Borghesani il 9 Gen 2013
MATLAB does not support arrays of function handles (some earlier versions did what version are you using?). Use a cell array containing the function handles then you should be able to use the line:
A(l,c)=A(l,c)+(b{l}(D(1,h))*b{c}(D(1,h)))
Note changing ( to { for accessing b.

Più risposte (3)

Daniel Shub
Daniel Shub il 9 Gen 2013
What i whant to do here is calculate the value of the functon b(l) on D(l,h)
The simple answer is that despite the fact that you want to do this, you cannot. There are hacks, but the simplest way is
temp1 = b(l);
temp2 = b(c);
A(l,c)=A(l,c)+temp1(D(1,h))*temp2(D(1,h));
  2 Commenti
Misa
Misa il 9 Gen 2013
I tried that but I get another error:
??? Attempted to access temp1(-2); index must be a positive integer or logical.
So I think 'it' isnt recognizing temp1 as a function but as an array
Walter Roberson
Walter Roberson il 9 Gen 2013
Modificato: Walter Roberson il 9 Gen 2013
What datatype is b ? What does class(b) show ?

Accedi per commentare.


Jan
Jan il 9 Gen 2013
Modificato: Jan il 9 Gen 2013
If you use a modern Matlab version, at least >= 2009a, you can store function handles in a cell only. Then this should work:
A(l,c) = A(l,c) + (b{l}(D(1,h)) * b{c}(D(1,h)))
Using an array for function handles has been available for a short time only, because it causes an ambiguity for e.g. "F(1)": Is (1) the argument of the function or the index of the functionhandle array?

Utaibah Mustafa
Utaibah Mustafa il 2 Dic 2016
Modificato: Walter Roberson il 2 Dic 2016
??? Error: ()-indexing must appear last in an index expression
p=0.05;
i=0.01;
d=0;
sim('utaibah')
plot(time,PV)
hold on
p=0.1;
i=0.01;
d=0;
sim('utaibahm')
plot(time,PV,'r')
p=0.2;
i=0.01;
d=0;
sim('utaibahmus')
plot(time,PV,'k')
  2 Commenti
Walter Roberson
Walter Roberson il 2 Dic 2016
You appear to be using sim() on three Simulink models. Do one or more of them have MATLAB Function Blocks, or arithmetic blocks, or Level 2 Functions programmed in MATLAB? The problem is probably in one of the models.
We will probably need the three models to test with.
Question: does utaibah have a To Workspace block that is creating PV, or did you define PV in something you did not show here?

Accedi per commentare.

Categorie

Scopri di più su Data Type Identification in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by