How do you set a default reference for structure fields.

5 ビュー (過去 30 日間)
Eric
Eric 2015 年 6 月 18 日
編集済み: Stephen23 2015 年 6 月 19 日
Suppose I have the structure "structure1" that has 3 fields a, b, c. I pass this structure to a function to perform some operations. I would like to setup the function as follows
function out = function(structure1)
% set structure 1 as the default field reference
?command? structure1
out = a + b*c;
I am trying to avoid
out = structure1.a + structure1.b*structure.c;
or
a = structure1.a;
b = structure1.b;
c = structure1.c;
out = a+b*c;
I seem to remember there was a command that did this in release 13 but I just cannot remember the command itself.
Thanks for your help,
Eric

採用された回答

Tim
Tim 2015 年 6 月 18 日
There's this one from the file exchange that does what you want:
I think you would just do:
v2struct(structure1);
And it would pull a b and c as variables.
  1 件のコメント
Eric
Eric 2015 年 6 月 19 日
編集済み: Eric 2015 年 6 月 19 日
Thanks, I will look into it.

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

その他の回答 (1 件)

Stephen23
Stephen23 2015 年 6 月 18 日
編集済み: Stephen23 2015 年 6 月 19 日
You could use struct2cell and deal to allocate those field values to separate variables, but this depends on the order in which the fields were defined:
>> S.a = 1;
>> S.b = 2;
>> S.c = 3;
>> C = struct2cell(S);
>> [a,b,c] = deal(C{:})
a =
1
b =
2
c =
3
The most robust solution would be as you wrote in your question:
a = structure1.a;
b = structure1.b;
c = structure1.c;
  1 件のコメント
Eric
Eric 2015 年 6 月 19 日
Thank you. I will probably go with the second part sin alreadyce that is what I have already. The data structure I am dealing with is quite large and the work has been done already, I was just hoping to cut down on the number of lines and speed the script up.

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

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by