MATLAB equivalent to VBA's 'With' Statement

24 ビュー (過去 30 日間)
Brittany
Brittany 2020 年 4 月 1 日
コメント済み: Walter Roberson 2020 年 4 月 8 日
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

採用された回答

Walter Roberson
Walter Roberson 2020 年 4 月 1 日
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 件のコメント
Brittany
Brittany 2020 年 4 月 8 日
Do you have any suggestions for the second example I added?
Walter Roberson
Walter Roberson 2020 年 4 月 8 日
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));

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by