MATLAB equivalent to VBA's 'With' Statement

24 visualizaciones (últimos 30 días)
Brittany
Brittany el 1 de Abr. de 2020
Comentada: Walter Roberson el 8 de Abr. de 2020
Is there a MATLAB equivalent to VBA's 'With' statement?
For example, is there a way to write
level1.level2(3).time.best = something
level1.level2(3).time.worst = somethingElse
level1.level2(3).time.Avg = somethingAvg
in a simplified way such as
With level1.level2(3).time
.best = something
.worst = somethingElse
.Avg = somethingAvg
end with
Edit:
Are there any good ways to simplify retreiving and editing nested structure data?
Another example of what I would like to make cleaner:
level1.level2(3).time.best = level1.level2(3).time.something + whatever
level1.level2(3).time.worst = level1.level2(3).time.somethingElse + whatever2
level1.level2(3).time.Avg = somethingAvg

Respuesta aceptada

Walter Roberson
Walter Roberson el 1 de Abr. de 2020
No, there is not.
In the special case that you are changing all of the fields, you can do something like
level1.level2(3).time = struct('best', something, 'worst', somethingElse, 'Avg', somethingAvg);
  2 comentarios
Brittany
Brittany el 8 de Abr. de 2020
Do you have any suggestions for the second example I added?
Walter Roberson
Walter Roberson el 8 de Abr. de 2020
A number of years ago, I wrote a structure merge function that went something like:
function C = structmerge(A, B)
for fn = fieldnames(B)
A.(fn{1}} = B.(fn{1});
end
end
except mine was more complicated because it handled other situations as well.
The idea here would be that you would use
level1.level2(3).time = structmerge(level1.level2(3).time, struct('best', newbest, 'worst', newworst, 'Avg', newavg));

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Get Started with MATLAB en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by