好吧我似乎已经解决了大部分问题,我只是需要专家的眼睛来挑选我的错误,因为我被卡住了。
我有一个长度为[125 X 27]
的文件,我想将其转换为长度为[144 x 27]
的文件。现在,我想替换丢失的文件(时间戳)行的零。 (理想情况下,每日平均10分钟,因此文件长度应为144)
以下是我正在使用的代码:
fid = fopen('test.csv', 'rt');
data = textscan(fid, ['%s' repmat('%f',1,27)], 'HeaderLines', 1, 'Delimiter', ',');
fclose(fid);
%//Make time a datenum of the first column
time = datenum(data{1} , 'mm/dd/yyyy HH:MM')
%//Find the difference in minutes from each row
timeDiff = round(diff(datenum(time)*(24*60)))
%//the rest of the data
data = cell2mat(data(2:28));
newdata=zeros(144,27);
for n=1:length(timeDiff)
if timeDiff(n)==10
newdata(n,:)=data(n,:);
newdata(n+1,:)=data(n+1,:);
else
p=timeDiff(n)/10
n=n+p;
end
end
有人可以帮助我找到for
循环中的错误。我的输出文件似乎错过了几个带时间戳的值。
%********************************************** ************************************************** ************************************************** ***********************************
有人可以帮我弄清楚读取上述文件吗?
我正在替换
fid = fopen('test.csv','rt');
data = textscan(fid,['%s'remat('%f',1,27)],'HeaderLines',1,'Delimiter',',');
FCLOSE(FID);
使用
[c,pathc] = uigetfile({'* .txt'},'选择文件','C:\ data');
file = [pathc c];
file = textscan(c,['%s'remat('%f',1,27)],'HeaderLines',1,'Delimiter',',');
它不起作用
%
旧问题的新增内容
p = 1; %index到目的地
对于n = 1:长度(timeDiff)
%if if timeDiff(n)== 10
%newfile(p,:) = file(n,:);
%newfile(p + 1,:) =文件(n + 1,:);
%p = p + 1;
%其他 %p = p +(timeDiff(n)/ 10);
%endQ = cumsum(timeDiff测量(N)/ 10);
如果q == 1
newfile中(P,:)=文件(N,:);
P = P + 1;
否则
p = p +(timeDiff(n)/ 10);结束
结束
xlswrite( 'testnewws11.xls',newfile中);
即使使用cumsum命令,当我的文件在长期缺失的文件中间有1,2个时间戳时,此代码也会失败 例子
2009年8月16日0:00 5.34
8/16/2009 0:10 3.23
8/16/2009 0:20 2.23
8/16/2009 0:30 1.23
8/16/2009 0:50 70
8/16/2009 2:00 5.23
8/16/2009 2:20 544
8/16/2009 2:30 42.23
8/16/2009 3:00 71.23
8/16/2009 3:10 3.23
我的输出看起来像
5.34
3.23
2.23
0
0
0
0
0
0
0
0
0
5.23
544.
42.23
0
0
0
3.23
有什么想法吗?
答案 0 :(得分:2)
更新新版问题
您似乎误解了我建议的cumsum
解决方案的意图。您不再需要循环,因为cumsum
会为您计算最终的余数。但是,我遗漏了一个关键部分 - 第一个索引仍然需要从数据文件中确定。用以下内容替换for循环:
[y,m,d] = datevec(time(1)); %# get the year, month and day corresponding to the first recorded timestamp
firstidx = time(1)-datenum(y,m,d,0,0,0)+1; %# get the offset from midnight to the first recorded timestamp
q = cumsum([firstidx ; round(diff(time)*24*60)/10]); %# these are the indeces into newdata
newdata(q,:) = data;
以前的答案
您正在使用n
索引新数据和数据,并根据长度(timeDiff)停止索引。这意味着你的循环永远不会触及超长的newData元素(timeDiff)。另外,我根本不了解newdata(n+1,)...
行的作用,因为它通常会在下一次迭代中被覆盖。我认为你需要的是:
p = 1; %index into destination
for n = 1:length(timeDiff)
if timeDiff(n) == 10
newdata(p,:) = data(n,:);
p = p + 1;
else
p = p + timeDiff(n)/10;
end
end
你可以通过以下方式使这个看起来更整洁:
p = cumsum(timeDiff(n)/10); % vector of destination indeces
newdata(p) = data;
(我实际上没有测试过这个......)
请注意,此方案取决于包含整数值的timeDiff
向量!你可能也需要在那里打电话给round
。
答案 1 :(得分:2)
关于第二个问题:
Uigetfile返回文件名,而不是文件ID。因此,你仍然需要调用fopen。
[c,pathc]=uigetfile({'*.txt'},'Select the file','C:\data');
file=[pathc c];
%# get a file ID
fid = fopen(file,'rt');
data= textscan(fid, ['%s' repmat('%f',1,27)], 'HeaderLines', 1, 'Delimiter', ',');
fclose(fid)