如何prevent在MATLAB结构阵列的创造? [英] How to prevent the creation of struct array in matlab?
问题描述
我试图做这样的结构
A =
A:{AAAAA}
乙:{'BB'}
块引用>这是确定的单元格的值分配给每个字段。
A = [];
A.A = {AA,AAA};
A·B = {'BB'};但是,如果我不喜欢这样。
A =结构('A',{'AA','AAA'},'B',{'BB'});
结果是一个结构数组
A =
块引用>
块引用>
1×2结构数组的字段:
A
b
一(1)
块引用>
块引用>
ANS =
A:'AA'
乙:BB
一(2)
块引用>
块引用>
ANS =
A:'AAA'
乙:BB
块引用>什么是使用做出这样一个结构的正确方法
结构()
?解决方案这是的记录的行为:
S =结构(场,值)
创建一个具有指定的结构数组
场和价值观。在值
输入参数可以是任何数据类型,例如
作为数字,逻辑,文字,或电池阵列。
如果值是一个单元阵列,那么
取值
是一个结构数组具有相同尺寸值
。取值
的每个元素都包含相应的
值的元素
。例如,S =结构('F',{'A','B'})
收益
S(1).F ='A'和s(2).F ='B'
。
块引用>有真的不是一个错误的方式做到这一点,这两个选项是完全没有问题。如果您想使用
结构
你需要你的窝单元阵列:A =结构('A',{{'AA','AAA'}},'B',{'BB'});
主要生产:
A = 答:{AAAAA}
乙:BBI'm trying to make a struct like this
a =
a: {'aa' 'aaa'} b: {'bb'}
It's ok to assign the cell value to each field.
a=[]; a.a = {'aa','aaa'}; a.b = {'bb'};
But if I do like this
a=struct('a',{'aa','aaa'},'b',{'bb'});
the result is a struct array
a =
1x2 struct array with fields:
a b
a(1)
ans =
a: 'aa' b: 'bb'
a(2)
ans =
a: 'aaa' b: 'bb'
What's the correct way to make such a struct using
struct()
?解决方案This is the documented behavior:
s = struct(field,value)
creates a structure array with the specified field and values. Thevalue
input argument can be any data type, such as a numeric, logical, character, or cell array.If value is a cell array, then
s
is a structure array with the same dimensions asvalue
. Each element ofs
contains the corresponding element ofvalue
. For example,s = struct('f',{'a','b'})
returnss(1).f = 'a' and s(2).f = 'b'
.There really isn't a wrong way to do it, either option is perfectly fine. If you'd like to use
struct
you will need to nest your cell array:a = struct('a', {{'aa','aaa'}}, 'b', {'bb'});
Which produces:
a = a: {'aa' 'aaa'} b: 'bb'
这篇关于如何prevent在MATLAB结构阵列的创造?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!