From: Benedikt Heudorfer on 18 Jun 2010 08:15 my script is the following: clear all; fid = fopen('Q_HK_Bechernbach.txt','r'); file = textscan(fid, '%f %17c %f %f','headerLines', 1, 'Delimiter',','); date=datenum(file{2}, 'dd.mm.yyyy HH:MM'); lgt=length(date); work(:,1)=file{1,4}*0.001; % generate daily timeserie stime=floor(date(1)); etime=floor(date(lgt)); difftime=etime-stime; steps=difftime/1; timeline=zeros(2,1); timeline(1,1)=stime; dummy=stime; for i=2:steps+1 timeline(i,1)=dummy+1; dummy=timeline(i,1); end % calculating daily values lgt2=length(timeline); results=zeros(lgt2,1); for a=1:lgt2 for i=1:lgt if a<lgt2 if date(i)>=timeline(a) && date(i)<timeline(a+1) results(a,1)=results(a,1)+work(i,1); end else for b=1:lgt if date(b)>=timeline(lgt2) results(lgt2,1)=results(lgt2,1)+work(b,1); end end end end end %fid=fopen('Bechernbach_Tageswerte.txt', 'w'); %fprintf(fid,'%16d %f\n',date, results(:,1)); %fclose(fid); everything works except the saving into .txt. (last 3 lines with the %...) i want 2 columns: first with the date, second with data from results(:,1)
From: Wayne King on 18 Jun 2010 08:43 "Benedikt Heudorfer" <rawker_(a)web.de> wrote in message <hvfo0n$eu4$1(a)fred.mathworks.com>... > my script is the following: > > > > clear all; > fid = fopen('Q_HK_Bechernbach.txt','r'); > file = textscan(fid, '%f %17c %f %f','headerLines', 1, 'Delimiter',','); > date=datenum(file{2}, 'dd.mm.yyyy HH:MM'); > > lgt=length(date); > work(:,1)=file{1,4}*0.001; > > % generate daily timeserie > stime=floor(date(1)); > etime=floor(date(lgt)); > difftime=etime-stime; > steps=difftime/1; > timeline=zeros(2,1); > timeline(1,1)=stime; > dummy=stime; > for i=2:steps+1 > timeline(i,1)=dummy+1; > dummy=timeline(i,1); > end > > % calculating daily values > lgt2=length(timeline); > results=zeros(lgt2,1); > for a=1:lgt2 > for i=1:lgt > if a<lgt2 > if date(i)>=timeline(a) && date(i)<timeline(a+1) > results(a,1)=results(a,1)+work(i,1); > end > else for b=1:lgt > if date(b)>=timeline(lgt2) > results(lgt2,1)=results(lgt2,1)+work(b,1); > end > end > end > end > end > > %fid=fopen('Bechernbach_Tageswerte.txt', 'w'); > %fprintf(fid,'%16d %f\n',date, results(:,1)); > %fclose(fid); > > > > everything works except the saving into .txt. (last 3 lines with the %...) > i want 2 columns: first with the date, second with data from results(:,1) Hi Benedikt, Without going through all your code in detail, I see you have a vector of serial date numbers and data, how about this: Form a 2xN matrix with the first row your date number and the second row your data x=1:10; y =1:10; z = [x;y]; fprintf('%3d\t %f\n',z); Obviously, you have to substitute in your code: % I'm assuming that date is a column vector Data = [date'; results(:,1)']; fid=fopen('Bechernbach_Tageswerte.txt', 'w'); fprintf(fid,'%16d\t %f\n', Data); fclose(fid); I think it should work without the \t escape character, so try it both ways. Wayne
From: Walter Roberson on 18 Jun 2010 08:54 Benedikt Heudorfer wrote: > date=datenum(file{2}, 'dd.mm.yyyy HH:MM'); > %fid=fopen('Bechernbach_Tageswerte.txt', 'w'); > %fprintf(fid,'%16d %f\n',date, results(:,1)); > %fclose(fid); > everything works except the saving into .txt. (last 3 lines with the %...) > i want 2 columns: first with the date, second with data from results(:,1) Your date is set as a datenum of a value that includes hours and minutes, and datenum are in serial date format which is days and fractions of a date. Therefore your date will contain fractions, and is thus not suitable for printing out with a %16d format. The easiest/clearest way for you to get the printout you want is to use a loop, but you could also code fprintf(fid,'%16d %f\n', horzcat(date(:), results(:,1)) .'); The .' there is crucial to get it to work. (I did not try to fix up your date format here as I don't know what you want.)
From: Benedikt Heudorfer on 18 Jun 2010 09:37 "Wayne King" <wmkingty(a)gmail.com> wrote in message <hvfpko$q1b$1(a)fred.mathworks.com>... > "Benedikt Heudorfer" <rawker_(a)web.de> wrote in message <hvfo0n$eu4$1(a)fred.mathworks.com>... > > my script is the following: > > > > > > > > clear all; > > fid = fopen('Q_HK_Bechernbach.txt','r'); > > file = textscan(fid, '%f %17c %f %f','headerLines', 1, 'Delimiter',','); > > date=datenum(file{2}, 'dd.mm.yyyy HH:MM'); > > > > lgt=length(date); > > work(:,1)=file{1,4}*0.001; > > > > % generate daily timeserie > > stime=floor(date(1)); > > etime=floor(date(lgt)); > > difftime=etime-stime; > > steps=difftime/1; > > timeline=zeros(2,1); > > timeline(1,1)=stime; > > dummy=stime; > > for i=2:steps+1 > > timeline(i,1)=dummy+1; > > dummy=timeline(i,1); > > end > > > > % calculating daily values > > lgt2=length(timeline); > > results=zeros(lgt2,1); > > for a=1:lgt2 > > for i=1:lgt > > if a<lgt2 > > if date(i)>=timeline(a) && date(i)<timeline(a+1) > > results(a,1)=results(a,1)+work(i,1); > > end > > else for b=1:lgt > > if date(b)>=timeline(lgt2) > > results(lgt2,1)=results(lgt2,1)+work(b,1); > > end > > end > > end > > end > > end > > > > %fid=fopen('Bechernbach_Tageswerte.txt', 'w'); > > %fprintf(fid,'%16d %f\n',date, results(:,1)); > > %fclose(fid); > > > > > > > > everything works except the saving into .txt. (last 3 lines with the %...) > > i want 2 columns: first with the date, second with data from results(:,1) > > Hi Benedikt, Without going through all your code in detail, I see you have a vector of serial date numbers and data, how about this: > > Form a 2xN matrix with the first row your date number and the second row your data > > x=1:10; y =1:10; > z = [x;y]; > fprintf('%3d\t %f\n',z); > > Obviously, you have to substitute in your code: > > % I'm assuming that date is a column vector > > Data = [date'; results(:,1)']; > fid=fopen('Bechernbach_Tageswerte.txt', 'w'); > fprintf(fid,'%16d\t %f\n', Data); > fclose(fid); > > I think it should work without the \t escape character, so try it both ways. > > Wayne thank you very very much. it partially helped. It does write values into a .txt now, but in a wrong order. with your script, i write: data = [date,results(:,1)]; fid=fopen('Bechernbach_Tageswerte.txt', 'w'); fprintf(fid,'%16d\t %f\n',data); fclose(fid); Problem now: first it writes all dates in both column, and then below that all results in both columns. like this: 733165 733166.000000 733167 733168.000000 733169 733170.000000 733171 733172.000000 733173 733174.000000 .... 734149 734150.000000 734151 734152.000000 2.970035e-02 0.118471 1.226650e-01 0.124119 1.258972e-01 0.381086 whats wrong now?
From: dpb on 18 Jun 2010 09:50 Benedikt Heudorfer wrote: .... > data = [date,results(:,1)]; > fid=fopen('Bechernbach_Tageswerte.txt', 'w'); > fprintf(fid,'%16d\t %f\n',data); > fclose(fid); > > Problem now: first it writes all dates in both column, and then below > that all results in both columns. like this: > 733165 733166.000000 > ... > 734151 734152.000000 > 2.970035e-02 0.118471 .... > whats wrong now? Order...Matlab stores in _COLUMN_MAJOR_ order and so writes the array in column major order. Walter's response earlier addressed that... One nit first, depending on the platform it can be significant; I'd get into the habit of always specifying the 't' for file type in the fopen() statement for text file... fid=fopen('Bechernbach_Tageswerte.txt', 'wt'); Now, to write the data in the order you wish, transpose the array... fprintf(fid,'%16d\t %f\n',data'); ' Note the "'" to transpose array This is general lesson--"column-major storage" :) --
|
Next
|
Last
Pages: 1 2 Prev: Stateflow Transition (MultiLine) Next: uiwait multiple figure interface |