简体   繁体   English

数据的字符串标题Matlab

[英]String Header for data Matlab

I found "Adding a header to a matrix in Matlab" question which was very close to what I need done. 我发现“在Matlab中向矩阵添加标头” 问题与我需要完成的工作非常接近。 I was hoping to keep the data in the cells, since my headers are large and i'm running this multiple times I dont want to use file io since it adds tons of time. 我希望将数据保留在单元格中,因为我的标头很大,而且我多次运行此程序,所以我不想使用文件io,因为它会增加大量的时间。

this is what i have... 这就是我所拥有的...

  • header = ( 'Quarter', 'monthly amount remaining', 'annual amountremaining'); 标头=(“季度”,“每月剩余金额”,“年度剩余金额”);

    data = 数据=

      1 30000 150000 2 20000 130000 

and i can't seem to get this 我似乎无法得到这个

out = 

  Quarter    monthly am    annual am
        1         30000       150000

        2         20000      130000

It's very frustrating, i've tried num2str, and a bunch of other stuff... i'm going to try num2cell and just make a big array and fill them in... well happy friday, i'm heading home T_T 这非常令人沮丧,我尝试了num2str以及其他一些功能……我要尝试使用num2cell并制作一个大型数组并将其填充...很好,星期五,我要回家T_T

If you have access to the Statistics Toolbox, you can create a dataset array 如果有权访问统计信息工具箱,则可以创建数据集数组

header = {'Quarter', 'monthly amount remaining', 'annual amountremaining'}
data =    [ 1        30000    150000;
    2        20000    130000];

ds = dataset({data,header{:}})

ds = 

    Quarter    monthlyAmountRemaining    annualAmountremaining
    1          30000                     1.5e+05              
    2          20000                     1.3e+05  

Note that this removes the spaces in your header names, but with the dataset, you can then use these names to conveniently access the columns, such as: 请注意,这会删除标题名称中的空格,但是对于数据集,您可以使用这些名称方便地访问列,例如:

>> ds.Quarter

ans =

     1
     2

If you just want to write to Excel (and not use the dataset methods), you can create a single cell array: 如果只想写入Excel(而不使用数据集方法),则可以创建一个单元格数组:

 [header;num2cell(data)]

ans = 

    'Quarter'    'monthly amount remaining'    'annual amountremaining'
    [      1]    [                   30000]    [                150000]
    [      2]    [                   20000]    [                130000]

As far as I am aware there is no true built in support for that. 据我所知,还没有真正的内置支持。 Related to what Xurtio mentioned, you're wanting to do in reverse what Matlab undoes when it performs an xlsread. 与Xurtio提到的内容相关,您想要反向执行Matlab在执行xlsread时撤消的操作。 Their solution is to create a matrix for the numbers (which have a fixed size and are therefore amenable to array style indexing), and a cell array for the strings which have a variable size. 他们的解决方案是为数字创建矩阵(大小固定,因此适合数组样式索引),为单元格创建大小可变的字符串。

The following is pretty much from the disp() Matlab docs and it accomplishes the effect, but not in a flexible way: 以下是Matp文档disp()的大部分内容,并且可以灵活地实现效果:

header='        Quarter  MonthlyAM   AnnualAM  ';
data=[1 30000 150000; 2 20000 130000];
disp(header);
disp(data);
    Quarter  MonthlyAM   AnnualAM  
       1       30000      150000
       2       20000      130000

If you want to achieve more flexible formatting, you could look into an sprintf intermediary for the data matrix. 如果要实现更灵活的格式设置,可以考虑使用sprintf中介作为数据矩阵。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM