WG-
WG-

Reputation: 1070

Matlab - With ... End structure

Does Matlab have a kind of with...end command? http://msdn.microsoft.com/en-us/library/wc500chb(v=vs.80).aspx

I have a variable in my workspace which contains a lot of nested data. Now I don't want to have to type this all the time:

Root.ChildLevel1.A = Root.ChildLevel1.B + Root.ChildLevel1.C

But rather something like:

with Root.ChildLevel1
  A = B + C
end

is this possible?

Upvotes: 4

Views: 104

Answers (2)

Dennis Jaheruddin
Dennis Jaheruddin

Reputation: 21563

I have to say that I would not recommend using this function very often, but I once tried a FEX contribution that allows you to unpack structs.

Of course this will still require you to update the struct after you have done the calculations so I only use it for subfunctions that mainly use the struct as input.

I am not sure, but I think this is the one I tried:

http://www.mathworks.com/matlabcentral/fileexchange/26216-structure-fields-to-variables

Upvotes: 1

Shai
Shai

Reputation: 114886

I'm not aware of such functionality in Matlab.
What you can do is

cur = Root.ChildLevel1;
cur.A = cur.B + cur.C;

Edit:
According to comment by @Nick, if Root.ChildLevel1 is not subclass of handle, then one should add the following line:

Root.ChildLevel1 = cur;

I would also recommend to

clear cur; 

at the end.

Upvotes: 3

Related Questions