MATLAB Answers

Yingzhi
2

Is there something like a 'struct pointer' in Matlab?

Yingzhi
さんによって質問されました 2012 年 8 月 28 日
Let's assume that I have a pretty deep structure called dataPool and I'm currently at level 'dataPool.instrument.profile.experiment'. Every time I want to change something I have to write 'dataPool.instrument.profile.experiment.xxx = ...;'. My question is, can I somehow make a reference to it, let's call it 'exp', so that 'exp' delegates all the changes for 'dataPool.instrument.profile.experiment' ?
If I simply write 'exp = dataPool.instrument.profile.experiment;', everything is copied by value, which means when I write 'exp.inputChannel = blahBlah', a new field 'inputChannel' would be created for 'exp' instead of 'dataPool.instrument.profile.experiment'.
Could some one help me on this? Thanks!

  0 件のコメント

サインイン to comment.

4 件の回答

Daniel Shub
回答者: Daniel Shub
2012 年 8 月 29 日
 採用された回答

I disagree with Walter and think MATLAB does provide this functionality in the OO HANDLE superclass. You can create a handle structure class
classdef hstruct < handle
properties
data
end
methods
function obj = hstruct(data)
obj.data = data;
end
end
end
Then if you can do
>> a.b.c = hstruct(1);
>> d = a.b.c;
>> d.data = 2;
>> a.b.c.data
ans =
2

  1 件のコメント

Yingzhi
2012 年 8 月 29 日
That's what I'm looking for. Thank you!

サインイン to comment.


Walter Roberson
回答者: Walter Roberson
2012 年 8 月 28 日

That facility is not present in MATLAB.

  0 件のコメント

サインイン to comment.


Jan
回答者: Jan
2012 年 8 月 28 日
編集済み: Jan
2012 年 8 月 28 日

No, exp = dataPool.instrument.profile.experiment does not make a deep data copy, but the fields of exp are shared data copies. Then this adds two new fields:
exp.fieldA = 1:100;
exp.fieldB.fieldB1 = rand(100);
Then the temporary exp is copied back again:
dataPool.instrument.profile.experiment = exp;
This creates again shared data copied, such that the memory for the data is not duplicated. In some usual test scenarios this is faster than:
% Slower:
dataPool.instrument.profile.experiment.fieldA = 1:100;
dataPool.instrument.profile.experiment.fieldB.fieldB1 = rand(100);
The overhead for addressing the substructs causes the delay. But the timings can and will depend on the Matlab version and most likely the number of fields and the depth of the nested structure. Because the field addressing does not require a lot of time, the differences in the timings are small and the example "rand(100)" will be more expensive. But when exp contains large data already, the shared data copy method help to reduce the processing time.

  4 件のコメント

Jan
2012 年 8 月 28 日
Yes, this is what I suggest and yes, this is not an real call by reference - as Walter has stated already. While you can simulate a reference mechanism in a C-Mex function, it is very likely that this will interfere with the shared data copy mechanism used for the copy-on-write strategy.
Using a temporary copy of the nested field will most likely save more programming and debugging time than run time. Nevermind, as long as this saves the time until the results are ready, I'm happy with it.
Yingzhi
2012 年 8 月 28 日
'Using a temporary copy of the nested field will most likely save more programming and debugging time than run time.' I second that. Thank you!
James Tursa
2012 年 8 月 28 日
FYI, regarding C-mex routines, it is not always possible to even tell when a field element (or cell element) is shared with another variable via a shared parent. MATLAB provides no functions in the API to tell you, and hacking into the variable itself doesn't always tell you either.

サインイン to comment.


Darik
回答者: Darik
2012 年 8 月 28 日

This is probably a bad idea but maybe it could come in handy in some situations:
dataPool = dynamicprops;
dataPool.addprop('instrument');
dataPool.instrument = dynamicprops;
dataPool.instrument.addprop('profile');
dataPool.instrument.profile = dynamicprops;
dataPool.instrument.profile.addprop('experiment');
dataPool.instrument.profile.experiment = dynamicprops;
exp = dataPool.instrument.profile.experiment;
exp.addprop('inputChannel');
exp.inputChannel = 8;
>> dataPool.instrument.profile.experiment.inputChannel
ans =
8

  0 件のコメント

サインイン to comment.



Translated by