获取 CSV 文件并按天将其保存到单独的 MATLAB 文件中
Posted
技术标签:
【中文标题】获取 CSV 文件并按天将其保存到单独的 MATLAB 文件中【英文标题】:Take CSV file and save it into separate MATLAB files by day 【发布时间】:2016-04-11 04:54:33 【问题描述】:我是 Matlab 新手,我需要在文件中为每一天创建 .mat 文件。 link to CSV Files
代码如下,我遇到的问题在注释掉的部分。我基本上需要为每一天保存一个单独的 .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
【问题讨论】:
你们有在线的 tester.csv 和 header.csv 吗? 【参考方案1】:你可以这样做:
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
【讨论】:
那么你的文件一定有问题,因为它显然对我有用。csvread
没有任何问题。只是它只接受数值。您的标题不是数字。如果您删除标题,您也许可以使用csvread
,但我会质疑为什么您有一个带有标题的文件和一个带有数据的文件,当您可以组合它们时。最终,你应该做最适合你的事情。【参考方案2】:
假设我们有.csv
文件
header.csv
Date,Time,Data1,Data2,...,DataN
tester.csv
Date,Time,Data1,Data2,...,DataN
DtNO,TiNO, D11, D12, ... ,D1N
...
然后运行此代码,我们将整个.csv
加载到一个数组中,按Date
列将其分组,然后将其导出到testdata-<Date>.mat
文件中。
Header=importdata('header.csv',',');
Testdata=importdata('tester.csv',',',1);
Date=Testdata(1,1);
EndDate=Testdata(end,1);
Advance=true;
while Advance
%% Find the part for one Date
Indices=find(Testdata(:,1)==Date);
%% Pick whole data set for perticullar Date
Data=Testdata(Indices,:);
%% Save Data, Header and Date variables only
save(['testdata-' Date '.mat','Data','Date','Header');
%% Advance to next Date
if Date~=EndDate
Date=TestData(Indices(end),1);
else
Advance=false;
end
end
【讨论】:
以上是关于获取 CSV 文件并按天将其保存到单独的 MATLAB 文件中的主要内容,如果未能解决你的问题,请参考以下文章