Hello, I am trying to create a function as follows with a set of variables that I get from a table named Change1:
X = [Change1.Ones Change1.ReturnsMarket]
func = @regress(Change1.ReturnsCleaned,X);
However, everytime I get the following error message:
Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for
mismatched delimiters.
Thank you all for your help !

 Respuesta aceptada

Star Strider
Star Strider el 24 de Feb. de 2020

0 votos

Try this:
func = @(Q)regress(Q,X);
then call it as:
[b,bint] = func(Change1.ReturnsCleaned);
(It seems a bit inefficient to me to wrap regress in an anonymous function. Personal opinion only.)

Más respuestas (2)

Walter Roberson
Walter Roberson el 24 de Feb. de 2020

0 votos

The @ operator can be used in four related syntaxes:
@NAME : create handle to function indicated by the name
@NAME1.NAME2 : create handle to function whose name inside its Package is as given
@() EXPRESSION : create handle to anonymous function with no parameters that evaluates to the given expression when invoked
@(NAME LIST) EXPRESSION : create handle to anonymous function with dummy parameters named inside the ()
You however have @EXPRESSION which is not valid syntax.
@ always has to do with creating handles, except possibly for some class definition purposes. If you want to invoke regress function instead of creating a handle then leave out the @
Nabil Benhadda
Nabil Benhadda el 24 de Feb. de 2020

0 votos

It is because I am trying to use it as part of the splitapply function, can I do so without the @?

6 comentarios

Star Strider
Star Strider el 24 de Feb. de 2020
To use it with splitapply, I would just use:
func = @(Q)regress(Q,X);
Nabil Benhadda
Nabil Benhadda el 24 de Feb. de 2020
Thank you a lot !
However now I have an issue, I use the code as following:
Change1.Groups = findgroups(Change1.Numero);
X = [Change1.Groups Change1.Ones Change1.ReturnsMarket];
Q = Change1.ReturnsCleaned;
func = @(Q)regress(Q,X);
Regress = splitapply(func,X,Q);
I get the following error message:
Error using splitapply (line 61)
Group numbers must be a vector of positive integers, and cannot be a sparse vector.
Error in Untitled (line 8)
Regress = splitapply(func,X,Q);
Star Strider
Star Strider el 24 de Feb. de 2020
I cannot help you with that. I only provided the correct function syntax in my Answer.
Nabil Benhadda
Nabil Benhadda el 24 de Feb. de 2020
Thank you a lot !
Star Strider
Star Strider el 24 de Feb. de 2020
As always, my pleasure!
Walter Roberson
Walter Roberson el 25 de Feb. de 2020
Q = Change1.ReturnsCleaned;
You are using that as your grouping variable, but it is not obvious to us that it contains only positive integers.
Change1.Groups = findgroups(Change1.Numero);
X = [Change1.Groups Change1.Ones Change1.ReturnsMarket];
findgroups does return the kind of positive integers needed for a grouping variable, but you are folding that information in as part of X and using X as the variable to be split rather than as the grouping variable. That seems odd.

Iniciar sesión para comentar.

Categorías

Preguntada:

el 24 de Feb. de 2020

Comentada:

el 25 de Feb. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by