如何prevent在MATLAB结构阵列的创造? [英] How to prevent the creation of struct array in matlab?

查看:120
本文介绍了如何prevent在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}
    乙:BB

I'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. The value 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 as value. Each element of s contains the corresponding element of value. For example, s = struct('f',{'a','b'}) returns s(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屋!

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