can a struct function access struct elements ?

1 visualización (últimos 30 días)
Andy
Andy el 1 de Abr. de 2020
Editada: Rik el 1 de Abr. de 2020
myStruct.va1 = 1
myStruct.val2 = 2
myStruct.mySum = @(x,y) x + y
nonetheless, x & y are specific inputs into the function.
if we don't pass an inline function, rather an existing myFun.m defined function, would the function be able to access any of the other struct fields without specifically feeding them to it as function parameters? (this makes me thinking of objects, but - rather avoid definining classes, etc).

Respuesta aceptada

Rik
Rik el 1 de Abr. de 2020
Editada: Rik el 1 de Abr. de 2020
When defining an anonymous function it will have access to all the variables in scope. It doesn't matter how you store that anonymous function. If you don't use myStruct in the definition of your anonymous function, it will not be captured.
Appoximately the same thing happens for function handles (it should grab the handle to the function at that time, so if you change your current directory it should still use the old function).
If you want a changing value of mystruct.va1 to be available to your function you have two options:
  1. pass the updated value as an input
  2. create a class where va1 is a property

Más respuestas (0)

Categorías

Más información sobre Function Creation en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by