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 CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by