[初学笔记] matlab中 struct的用法,以及如何保存在xls中

来源:互联网 发布:js slice方法 mdn 编辑:程序博客网 时间:2024/06/05 10:04

在matlab中,可以用struct来对一个变量的信息进行管理,最后转变成cell格式,存储在xls中


假设


1 首先创建 struct

下面是代码


you.name = input ('\n\n please enter your name or nickname\n\n','s');
you.country = input ('\n\n Where are you from?\n\n','s');
you.age = input ('\n\n please enter your age\n\n','s');
you.gender = input ('\n\n please enter your gender\n\n','s');
you.major = input ('\n\n What''s your major\n\n','s');
dreamy.name = input ('\n\n please enter dreamy''s name or nickname\n\n','s');
dreamy.country = input ('\n\n Where is he/she from?\n\n','s');
dreamy.age = input ('\n\n please enter dreamy''s age\n\n','s');
dreamy.gender = input ('\n\n please enter dreamy''s gender\n\n','s');
dreamy.major = input ('\n\n dreamy''s major?\n\n','s');

最后的struct结果
>> you
you =
       name: 'kkkk'
        age: 'sss'
     gender: 'iii'
      major: 'llll'
    country: 'www'
>> dreamy
dreamy =
       name: 'aaaaaa'
        age: 'ppppppppppp'
     gender: 'oooooooooooo'
      major: 'nnnnnnnnnnnnnn'
    country: 'qqqqqqqq'




2  我们可以用whos来进行检查数据类型和大小

>> whos you
  Name      Size            Bytes  Class     Attributes
  you       1x1               914  struct             
>> whos dreamy
  Name        Size            Bytes  Class     Attributes
  dreamy      1x1               982  struct         


3 转成cell格式

可以help struct2cell
但是一定要注意转换之后是列cell还是行cell

>> youinfo = struct2cell(you);
dreamyinfo = struct2cell(dreamy);

>> youinfo
youinfo =
    'kkkk'
    'sss'
    'iii'
    'llll'
    'www'

>> dreamyinfo
dreamyinfo =
    'aaaaaa'
    'ppppppppppp'
    'oooooooooooo'
    'nnnnnnnnnnnnnn'
    'qqqqqqqq'

>> whos youinfo
  Name         Size            Bytes  Class    Attributes
  youinfo      5x1               594  cell              

>> whos dreamyinfo
  Name            Size            Bytes  Class    Attributes
  dreamyinfo      5x1               662  cell              
        

4 写入xls
请参考前面的文章


原创粉丝点击