Matlab cell length

Posted by AP on Stack Overflow See other posts from Stack Overflow or by AP
Published on 2010-02-17T22:26:59Z Indexed on 2010/03/12 4:17 UTC
Read the original article Hit count: 281

Filed under:
|

Ok I seem to have got the most of the problem solved, I just need an expert eye to pick my error as I am stuck.

I have a file of length [125 X 27] and I want to convert it to a file of length [144 x 27]. Now, I want to replace the missing files (time stamps) rows of zeros. (ideally its a 10 min daily average thus should have file length of 144)

Here is the code I am using:

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

Can somebody please help me to find the error inside my for loop. My output file seems to miss few timestamped values.

%***********************************************************************************************************

Can somebody help me to figure out the uiget to read the above file??

i am replacing

fid = fopen('test.csv', 'rt');

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

fclose(fid);

With

[c,pathc]=uigetfile({'*.txt'},'Select the file','C:\data');

file=[pathc c];

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

And its not working

%


NEW ADDITION to old question

p = 1; %index into destination
for n = 1:length(timeDiff)
% if timeDiff(n) == 10
% newfile(p,:) = file(n,:);
% newfile(p+1,:)=file(n+1,:);
% p = p + 1;
% else
% p = p + (timeDiff(n)/10);
% end

q=cumsum(timeDiff(n)/10);
if q==1
newfile(p,:)=file(n,:);
p=p+1;
else
p = p + (timeDiff(n)/10);

end

end

xlswrite('testnewws11.xls',newfile);

even with the cumsum command this code fails when my file has 1,2 time stamps in middle of long missing ones example

8/16/2009 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

My output looks like

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

Any ideas?

© Stack Overflow or respective owner

Related posts about timestamp

Related posts about matlab