我是Matlab的新手,我需要每天在文件中创建.mat文件。链接到CSV文件
代码在下面,我所遇到的问题在注释掉的部分中。我基本上每天需要保存一个单独的.mat文件,该文件存储在第1列中。谢谢。
clear;
Info=csvread('tester.csv');
%header=csvread('header.csv');
days=unique(Info(:,1));
numdays= length(days);
for i = 1:numdays
%Info.header=header;
%write headers in
%Info.data=Info(Info(:,1)==days(i),:);
str = sprintf('save data%i.mat', i);
eval(str);
end
您可以执行以下操作:
fid = fopen('tester_small.csv','r'); % // open the file
h = fgetl(fid); % // read the first line which is the header
headers = strsplit(h,','); % // split the headers into a cell array
formatSpec = '%f%f%f%f%f%f'; % // set the format for the data
data = textscan(fid, formatSpec, 'Delimiter', ','); %// read in the data
fclose(fid); % // close the file
Info=cell2mat(data);
days=unique(Info(:,1));
numdays= length(days);
for cnt = 1:numdays
% // clear the variable between each run so
% // that you don't include data from previous iteration
clear S;
S.header = headers;
S.data=Info(Info(:,1)==days(cnt),:);
filename = sprintf('data%i.mat', cnt);
save(filename,'S');
end
这是tester_small.csv
文件的前十行:
Date,Minutes,DC114T,DCRCS103,DCRCS104,DCRCS110
40484,5,72.681519,692.999939,689.999878,559.999878
40484,10,72.681519,695.19989,693.799866,558.799927
40484,15,72.681519,693.999939,693.799866,560.999878
40484,20,72.681519,692.19989,694.999878,558.799927
40484,25,72.681519,693.19989,696.999939,558.999939
40484,30,72.681519,692.19989,699.999878,560.799927
40484,35,72.681519,690.999878,700.999878,563.999939
40484,40,72.681519,696.199951,697.999878,562.999939
40484,45,72.681519,696.199951,696.999939,564.999878
这是加载文件时的结果。
load data1.mat
>> S
S =
scalar structure containing the fields:
header =
{
[1,1] = Date
[1,2] = Minutes
[1,3] = DC114T
[1,4] = DCRCS103
[1,5] = DCRCS104
[1,6] = DCRCS110
}
data =
4.0484e+004 5.0000e+000 7.2682e+001 6.9300e+002 6.9000e+002 5.6000e+002
4.0484e+004 1.0000e+001 7.2682e+001 6.9520e+002 6.9380e+002 5.5880e+002
4.0484e+004 1.5000e+001 7.2682e+001 6.9400e+002 6.9380e+002 5.6100e+002
4.0484e+004 2.0000e+001 7.2682e+001 6.9220e+002 6.9500e+002 5.5880e+002
4.0484e+004 2.5000e+001 7.2682e+001 6.9320e+002 6.9700e+002 5.5900e+002
4.0484e+004 3.0000e+001 7.2682e+001 6.9220e+002 7.0000e+002 5.6080e+002
4.0484e+004 3.5000e+001 7.2682e+001 6.9100e+002 7.0100e+002 5.6400e+002
4.0484e+004 4.0000e+001 7.2682e+001 6.9620e+002 6.9800e+002 5.6300e+002
4.0484e+004 4.5000e+001 7.2682e+001 6.9620e+002 6.9700e+002 5.6500e+002
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句