MATLAB equivalent to VBA's 'With' Statement

24 vues (au cours des 30 derniers jours)
Brittany
Brittany le 1 Avr 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

Réponse acceptée

Walter Roberson
Walter Roberson le 1 Avr 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 commentaires
Brittany
Brittany le 8 Avr 2020
Do you have any suggestions for the second example I added?
Walter Roberson
Walter Roberson le 8 Avr 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));

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Get Started with MATLAB dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by