Labfans是一个针对大学生、工程师和科研工作者的技术社区。 论坛首页 | 联系我们(Contact Us)
MATLAB爱好者论坛-LabFans.com
返回   MATLAB爱好者论坛-LabFans.com > 其它 > 资料存档
资料存档 资料存档
 
 
主题工具 显示模式
旧 2019-12-10, 20:30   #1
poster
高级会员
 
注册日期: 2019-11-21
帖子: 3,006
声望力: 66
poster 正向着好的方向发展
帖子 Matlab单元格长度

好的,我似乎已经解决了大部分问题,我只需要专家的眼睛来选出我遇到的错误就可以了。

我有一个长度为[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');

数据= textscan(fid,['%s'repmat('%f',1,27)],'HeaderLines',1,'Delimiter',',');

fclose(fid);


[c,pathc] = uigetfile({'*。txt'},'选择文件','C:\ data');

file = [pathc c];

file = textscan(c,['%s'repmat('%f',1,27)],'HeaderLines',1,'Delimiter',',');

而且它不起作用



对旧问题的新添加
p = 1;目的地索引%
对于n = 1:length(timeDiff)
如果timeDiff(n)== 10,则为%
%newfile(p,:) = file(n,:);
%newfile(p + 1,:)= file(n + 1,:);
%p = p +1;
其他百分比
%p = p +(timeDiff(n)/ 10);
% 结束

q = cumsum(timeDiff(n)/ 10);
如果q == 1
newfile(p,:)= file(n,:);
p = p + 1;
其他
p = p +(timeDiff(n)/ 10);

结束

结束

xlswrite('testnewws11.xls',newfile);

即使使用cumsum命令,当我的文件在长时间丢失的示例中间有1,2个时间戳时,此代码也会失败
2009年8月16日0:00 5.34
2009年8月16日0:10 3.23
2009年8月16日0:20 2.23
2009年8月16日0:30 1.23
2009年8月16日0:50 70
2009年8月16日2:00 5.23
2009年8月16日2:20 544
2009年8月16日2:30 42.23
2009年8月16日3:00 71.23
2009年8月16日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

有任何想法吗?



回答:

更新新版本的问题

你似乎有误解的意图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在那里了。



更多&回答...
poster 当前离线   回复时引用此帖
 


发帖规则
不可以发表新主题
不可以发表回复
不可以上传附件
不可以编辑自己的帖子

启用 BB 代码
论坛禁用 表情符号
论坛启用 [IMG] 代码
论坛启用 HTML 代码



所有时间均为北京时间。现在的时间是 07:02


Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.