I am trying to generate text files from the following example:
clear all
Year = {'2007','2008','2009'};
for i = 1:length(Year)
StartDate{i} = [Year{i} '-01-01 00:00'];
EndDate{i} = [Year{i} '-12-31 23:57'];
Resolution = 60;
DateT{i} = datestr(datenum(StartDate{i},'yyyy-mm-dd HH:MM'):Resolution/(60*24):...
datenum(EndDate{i},'yyyy-mm-dd HH:MM'),'yyyy-mm-dd HH:MM');
DateTime{i} = cellstr(DateT{i});
end
%save as text file
for i = 1:length(Year);
mkdir(fullfile('C:',Year{i}));
filename{i} = fullfile('C:',Year{i},'DateTime.txt');
fid{i} = fopen(filename{i},'w+');
for ii = 1:length(DateTime);
a{i} = 1:length(DateTime{i});
for k = 1:a{i};
fprintf(fid{i},'%s',DateTime{i}{k});
end
end
end
fclose 'all';
I know that the error must be in the line containing fprintf but I dont know how to fix it. How is it possible to write the each cell of DateTime into separate text files as indicated in the script?

1 comentario

Mohammed Ibrahim
Mohammed Ibrahim el 10 de Nov. de 2016
I had similar problem, I used strcat after cellstr. so in this question this may work: DateTime{i} = strcat(cellstr(DateT{i}));

Iniciar sesión para comentar.

 Respuesta aceptada

Image Analyst
Image Analyst el 31 de Mzo. de 2012

0 votos

Replace the corresponding lines with these:
a{i} = length(DateTime{i})
for k = 1:a{i}
fprintf(fid{i},'%s\n',DateTime{i}{k});
% fprintf(1,'%s\n',DateTime{i}{k});
end
The problem was the 1: in the "a{i}" line, and no \n in the fprintf().

3 comentarios

Richard
Richard el 31 de Mzo. de 2012
great. Why doesn't this look like it does in matlab. The text file seems to just be all of the dates with no real order. I would have expected it to be in a vector format i.e one date under the other!
Image Analyst
Image Analyst el 31 de Mzo. de 2012
If you mean why it doesn't look the same when you fprintf to the file instead of the command window, it's because if you go to a file you need to use \r\n instead of just \n. I'm not really sure why that is the convention, it just is.
Jan
Jan el 10 de Nov. de 2016
Using \r\n is not a convention, but text files can have either "\n" or "\r\n" or even "\r" as line breaks. Only the Windows Editor cannot display the different line breaks correctly, but e.g. Matlab's editor or any other one I've seen handles the different styles correctly. Therefore the convention should be not to use the Editor shipped with Windows.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Characters and Strings en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 31 de Mzo. de 2012

Comentada:

Jan
el 10 de Nov. de 2016

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by