gpt4 book ai didi

matlab - 来自 matlab 的制表符分隔文本文件

转载 作者:太空宇宙 更新时间:2023-11-03 20:31:32 26 4
gpt4 key购买 nike

以下代码生成与我当前使用的类似的数据集:

clear all
a = rand(131400,12);
DateTime=datestr(datenum('2011-01-01 00:01','yyyy-mm-dd HH:MM'):4/(60*24):...
datenum('2011-12-31 23:57','yyyy-mm-dd HH:MM'),...
'yyyy-mm-dd HH:MM');
DateTime=cellstr(DateTime);
header={'DateTime','temp1','temp2','temp4','temp7','temp10',...
'temp13','temp16','temp19','temp22','temp25','temp30','temp35'};

我正在尝试将输出转换为一个变量(称为“数据”),即将标题作为第一行 (1,:),“DateTime”从第 2 行开始 (2:end,1) 并运行通过每一行,如果有意义的话,最后将 'a' 作为数据 (2:end,2:end)。因此,“DateTime”和“header”分别用作行和列的标题。在此之后,我需要将其保存到制表符分隔的文本文件中。

我希望我已经清楚地表达了我的尝试。

最佳答案

一个简单的方法,但可能不是最快的:

Data = [header; DateTime, num2cell(a)];
filename = 'test.txt';
dlmwrite(filename,1); %# no create text file, not Excel
xlswrite(filename,Data);

更新:看起来 xlswrite 实际上更改了 DateTime 值的格式,即使它写入文本文件也是如此。如果格式很重要,这里是更好、实际上更快的方法:

filename = 'test.txt';
out = [DateTime, num2cell(a)];
out = out'; %# our cell array will be printed by columns, so we have to transpose

fid = fopen(filename,'wt');
%# printing header
fprintf(fid,'%s\t',header{1:end-1});
fprintf(fid,'%s\n',header{end});
%# printing the data
fprintf(fid,['%s\t', repmat('%f\t',1,size(a,2)-1) '%f\n'], out{:});
fclose(fid);

关于matlab - 来自 matlab 的制表符分隔文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8989520/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com