MATLAB equivalent to VBA's 'With' Statement

30 Ansichten (letzte 30 Tage)
Brittany
Brittany am 1 Apr. 2020
Kommentiert: Walter Roberson am 8 Apr. 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

Akzeptierte Antwort

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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Get Started with MATLAB finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by