Borrar filtros
Borrar filtros

how to populate a cell with a loop

1 visualización (últimos 30 días)
Susan Santiago
Susan Santiago el 6 de Oct. de 2018
Editada: dpb el 7 de Oct. de 2018
This is what I have with my code so far.
a = dir;
i=3;
d = a(i).name;
A = zeros(40,55);
while i<=43
A = textscan(fopen(d),'%f %f %f %f %f %f %f %f %f %f %f %f %f %f %s %s %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %f %s %s %f %f %f %f %f %f %f %f', 'Delimiter',',','Headerlines',1);
A_time = A{1,1};
B_time = num2str(A_time);
x = datenum(B_time,'yyyymmddHHMM');
i=i+1;
end
I have 43 data files in my folder. when I run this, A becomes a 1x55 cell with data from the first file. I'd like to make it so that the A on the outside of the loop is populated with the cell data from each file folder. Please help

Respuesta aceptada

dpb
dpb el 6 de Oct. de 2018
Editada: dpb el 7 de Oct. de 2018
fmt=[repmat('%f',1,14) repmat('%s',1,2) repmat('%f',1,29) repmat('%s',1,2) repmat('%f',1,8)]; % legible (sorta') format string
d=dir('AppropriateString*.ext'); % setup dir() to return the wanted actual files
for i=1:length(d) % iterate over files found
fid=fopen(d(i).name),'r');
A(i)=textscan(fid,fmt,'Delimiter',',','Headerlines',1);
fid=fclose(fid);
end
Will leave with cell array of one row per file.
Which specific columns are date/time data? The two sets of '%s' I presume?
datenum has been deprecated; better to use datetime instead and easier than textscan would be to use readtable.
Let us know the actual date format/location in the files and we can write specific conversion for it, too...
ADDENDUM
OK, if the string fields are really just 'nan' or variant thereof, use numeric format for them and convert them on input as well.
...
fmt=['%{yyyyMMddHHmmss}D' repmat('%f',1,54)]; % legible format string
for i=1:length(d) % iterate over files found
fid=fopen(d(i).name),'r');
A{i}=textscan(fid,fmt,'Delimiter',',','Headerlines',1,'collectoutput',1);
fid=fclose(fid);
end
using datetime class rather than the deprecated datenum for the date field.
You might also consider readtable depending on what your next step(s) are with these data.
  4 comentarios
Susan Santiago
Susan Santiago el 7 de Oct. de 2018
that's just columns of NAN. The date is written as a number which is why in the code I posted, I convert it to a string and then get the serial date number
dpb
dpb el 7 de Oct. de 2018
Editada: dpb el 7 de Oct. de 2018
'NaN' will be read as NaN, don't need a string format for it; that's what made think those probably were/could be date/time fields, maybe.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Dates and Time en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by