动态分配结构字段名称与层次结构 [英] Dynamically assign structure field name with hierarchy

查看:135
本文介绍了动态分配结构字段名称与层次结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够在MATLAB(r2014a)中动态分配字段到结构数组,包括层次结构。我不知道如何最好地描述问题,除非通过以下示例。



我知道如何做到这一点:

  field_name1 ='bar1'; 
field_name2 ='bar2';
%...等
foo。(field_name1)= pi;
foo。(field_name2)= exp(1);
%...等

导致结构变量 foo 与字段 bar1 bar2



我想要做的是这样的:

  field_name1 ='bar1'; 
field_name2 ='bar2';
%...等
如果true_false_statement
extra_level ='';
else
extra_level ='baz。';
end
foo。([extra_level field_name1])= pi;
foo。([extra_level field_name2])= exp(1);
%...等

根据以前的条件,这些字段可以存储在结构的顶层,或者在子字段下。



(注意,如果我从头开始这样做,我会设计不同的东西,以避免这个问题,但是我正在修改一些代码,这是一个更大的工作流程的中间步骤,所以我必须保持数据结构一致。)



目前,代码看起来像这样:

  field_name1 ='bar1'; 
field_name2 ='bar2';
%...等
如果true_false_statement
foo。(field_name)= pi;
foo。(field_name)= exp(1);
%...等
else
foo.baz(field_name)= pi;
foo.baz(field_name)= exp(1);
%...等
end

字段,但是有很多字段,这导致大量的复制/粘贴代码。我已经尝试使 extra_level 一个空字段,并且我尝试使用形式的 field_name *。* 如上所述,但是这两个选项都会导致错误,因为结果不是有效的变量名。



有没有一个很好的方法这样做?

解决方案

我能想到的最简单的方式来实现你所期望的使用(中间代码块)是使用 setfield (这主要是一个包装器对于 subsasgn 这些天)和事实上,一个空的单元格阵列扩展到什么(我已经过去回答,我喜欢称之为功能):

 >> foo = struct(); 
>> extra_level = {};
>> foo = setfield(foo,extra_level {:},'bar1',1)
foo =
bar1:1

>> extra_level = {'baz'};
>> foo = setfield(foo,extra_level {:},'bar2',2)
foo =
bar1:1
baz:[1x1 struct]

> > foo.baz.bar2
ans =
2


I would like to be able to dynamically assign fields to a structure array, including hierarchy, in MATLAB (r2014a). I'm not sure how best to describe the problem except through the following example.

I know how to do this:

field_name1 = 'bar1';
field_name2 = 'bar2';
% ... etc.
foo.(field_name1) = pi;
foo.(field_name2) = exp(1);
% ... etc.

results in a structure variable foo with fields bar1 and bar2.

What I would like to be able to do is this:

field_name1 = 'bar1';
field_name2 = 'bar2';
% ... etc.
if true_false_statement
    extra_level = '';
else
    extra_level = 'baz.';
end
foo.([extra_level field_name1]) = pi;
foo.([extra_level field_name2]) = exp(1);
% ... etc.

where depending on a previous condition, the fields can be stored either in the top level of the structure or else under a sub-field.

(Note that if I was doing this from scratch, I would design things differently to avoid this problem. However, I am modifying some code that is the middle step in a much larger workflow, so I have to keep the data structure consistent.)

Currently, the code looks something like this:

field_name1 = 'bar1';
field_name2 = 'bar2';
% ... etc.
if true_false_statement
    foo.(field_name) = pi;
    foo.(field_name) = exp(1);
    % ... etc.
else
    foo.baz.(field_name) = pi;
    foo.baz.(field_name) = exp(1);
    % ... etc.
end

Perhaps fine for one or two field, but there are a lot of fields, which results in a lot of copy/pasted code. I have tried making extra_level an empty field, and I have tried making field_name of the form *.* as above, but both options throw an error because the result is not a valid variable name.

Is there a good way to do this?

解决方案

The simplest manner I can think of to achieve your desired use (the middle block of code) is to use setfield (which is mostly a wrapper for subsasgn these days) and the fact that an empty cell array expands to nothing (something I've answered about in the past and what I like to call a feature):

>> foo = struct();
>> extra_level = {};
>> foo = setfield(foo,extra_level{:},'bar1',1)
foo = 
    bar1: 1

>> extra_level = {'baz'};
>> foo = setfield(foo,extra_level{:},'bar2',2)
foo = 
    bar1: 1
     baz: [1x1 struct]

>> foo.baz.bar2
ans =
     2

这篇关于动态分配结构字段名称与层次结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆