Borrar filtros
Borrar filtros

renaming files to easily read it in to matlab problematic

3 visualizaciones (últimos 30 días)
Hello community,
i have a lot of files to rename, i want to do this because i want to read it into matlab with a loop and after that convert it into matrices etc....
The files are called this way
control(f) Jun 14, 2012 18-10-04.txt control(f) Jun 14, 2012 18-13-23.txt control(f) Jun 14, 2012 18-16-20.txt control(f) Jun 14, 2012 18-20-57.txt ... and so on.
I solved a similar problem where the files are called control(f) 1.txt control(f) 2.txt ... and so on.
with this code: b = 'control(f) '; f = '.txt'; ... for i = 1 : 486 j = j+1; var_end = num2str (i); data_str_control = [b,var_end,f]; ...
but i dont know how to handle the problem with the different times in the name of the file. The time in the file is not following a rule.
Thanks in advance,

Respuesta aceptada

Friedrich
Friedrich el 28 de Ag. de 2012
Editada: Friedrich el 28 de Ag. de 2012
Hi,
why renaming them? This is not needed. Use the dir command to get a struct array which contains also the filenames:
>> dir
. .. test.mat
>> files = dir
files =
3x1 struct array with fields:
name
date
bytes
isdir
datenum
>> for i=1:numel(files)
files(i).name
end
ans =
.
ans =
..
ans =
test.mat
You can use the isdir field of the struct to determine if the current element is really a file.
for i=1:numel(files)
if ~files(i).isdir
load(files(i).name) %or do whatever you like to do with that file
end
end
  2 comentarios
Florian
Florian el 28 de Ag. de 2012
Editada: Florian el 28 de Ag. de 2012
If I do it like you suggested:
files = dir;
for i=1:numel(files) files(i).name end
670x1 struct array with fields: name date bytes isdir datenum
... and all names are returned. Now my current folder is mixed with a lot of data - also files i dont need. I just need the txt files. How can I select those files, and choose a 480x1 vector of each file beginning at 34:2 (there is a header in each file i dont need), and after that safe this vectors in the first, second, third,... row of a matrice?
Thank you for your answer Friedrich !
Friedrich
Friedrich el 28 de Ag. de 2012
Maybe use the fileparts function to check the extension of your files:
[pathstr, name, ext] = fileparts(files(i).name)
if strcmp(ext,'txt')
%do something
end
Or simply copy all the files you need to load into a seperate folder. Then you now that dir returns needed files only.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre File Operations en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by