【问题标题】:Matlab cell lengthMatlab 单元格长度
【发布时间】:2011-01-18 02:22:01
【问题描述】:

好的,我似乎已经解决了大部分问题,我只需要专家的眼光来发现我遇到的错误。

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

data = 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; %index 到目的地
对于 n = 1:长度(timeDiff)
% 如果 timeDiff(n) == 10
% 新文件(p,:) = 文件(n,:);
% 新文件(p+1,:)=文件(n+1,:);
% p = p + 1;
% 其他
% p = p + (timeDiff(n)/10);
% 结束

q=cumsum(timeDiff(n)/10);
如果 q==1
新文件(p,:)=文件(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

有什么想法吗?

【问题讨论】:

    标签: matlab timestamp


    【解决方案1】:

    更新新版本问题

    您似乎误解了我建议的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)停止索引。这意味着您的循环将永远不会触及超出长度(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 的电话。

    【讨论】:

    • 哇非常感谢您发现该错误。但是,我确实必须添加行 >newdata(p+1,:)=data(n+1,:);因为我的 timeDiff 需要最后两个数字的差异,所以如果我不添加这一行,它会错过最后一个数据集。有了这个,它可以完美地工作。另一个相关的问题。您知道将标题和时间戳(第一列)列重写到新数据文件中的简单方法吗?谢谢
    • 对于标题行,我建议您简单地使用fgetlfprintf。不要忘记从textscan 中删除HeaderLines 项目!为了复制时间戳,你不能在打印newdata(i)时打印time(i)吗?
    • 非常感谢。你为什么我总是在某些文件上而不是在所有文件上出现 datenum 错误,尽管它们具有相同的格式。 ???在 174 DATENUM 处使用 ==> datenum 时出错失败。 ==> makefilelength144 在 18 时出错 = datenum(data{1} , 'mm/dd/yyyy HH:MM');原因:错误使用 ==> dtstr2dtnummx 将日期字符串转换为日期数字失败。
    • 试试disp(data{1}) - 有时这有助于发现细微的格式差异。
    • 我在 time= .... 中摆脱了 'mm/dd/yyyy HH:MM' ,它似乎读取了默认格式。谢谢
    【解决方案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)
    

    【讨论】:

    • Sweet ,谢谢 我在某些文件上仍有问题,它给了我错误>???在 174 DATENUM 处使用 ==> datenum 时出错失败。 ==> makefilelength144 在 33 时出错 = datenum(data{1} , 'mm/dd/yyyy HH:MM');原因:使用 ==> dtstr2dtnummx 时出错 将日期字符串转换为日期编号失败。 > 当我查看文件时,它看起来与其他没有错误的文件相同,有什么想法吗??
    猜你喜欢
    • 1970-01-01
    • 2017-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多