Labfans是一个针对大学生、工程师和科研工作者的技术社区。 | 论坛首页 | 联系我们(Contact Us) |
![]() |
![]() |
#1 |
高级会员
注册日期: 2019-11-21
帖子: 3,006
声望力: 66 ![]() |
![]()
好的,我似乎已经解决了大部分问题,我只需要专家的眼睛来选出我遇到的错误就可以了。
我有一个长度为[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循环内找到错误。我的输出文件似乎缺少一些带有时间戳的值。 %************************************************* ****************************************************** ****************************************************** ************************************ 有人可以帮我弄清楚uiget读取上面的文件吗? 我要更换 fid = fopen('test.csv','rt');用 [c,pathc] = uigetfile({'*。txt'},'选择文件','C:\ data');而且它不起作用 % 对旧问题的新添加 p = 1;目的地索引%即使使用cumsum命令,当我的文件在长时间丢失的示例中间有1,2个时间戳时,此代码也会失败 2009年8月16日0:00 5.34我的输出看起来像 5.34有任何想法吗? 回答: 更新新版本的问题 你似乎有误解的意图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索引新数据和数据,并基于length(timeDiff)停止索引。这意味着您的循环将永远不会触及超过length(timeDiff)的newData元素。另外,我根本不了解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在那里了。 更多&回答... |
![]() |
![]() |