What to do about zeros in filenames when importing data to cell using sprintf ...
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Peter Beringer
el 18 de Oct. de 2020
Comentada: Ameer Hamza
el 18 de Oct. de 2020
This seems like it would be a fairly common problem, yet I haven't found any documentation or on Answers relating to this. So if anyone has a handy link, that'd be great (maybe it's too specific).
I have ten datasets in *.MAT format named sequentially:
Position01.mat, Position02.mat ... through to ... Position10.mat
Importing Position01 to Position09 to a cell is no issue, but opening and importing "Position10.mat" is made tricky because the number in its filename is "formatted" differently; specifically, doesn't start with zero. So, I'm wondering if I can keep my filenames, which keep the files nicely organised and in order outside MATLAB and find a workaround for dealing with "Position10.mat".
I'm currently using these lines to import the files ...
NumFiles = 10;
AudioCell = cell(1, NumFiles);
for k = 1: NumFiles
Position = sprintf('Position0%d.mat', k);
AudioCell{k} = importdata(Position);
end
It imports Position01 - Position09 exactly how I want, but Position10 has me stumped. Is changing the *.MAT filenames to "Position1", "Position2" and so on, my only decent option? So ...
Position = sprintf('Position%d.mat', k);
Thanks in advance!
0 comentarios
Respuesta aceptada
Ameer Hamza
el 18 de Oct. de 2020
Editada: Ameer Hamza
el 18 de Oct. de 2020
No, this is not the only option. The correct way is to use format specifier of sprintf() to add zeros automatically
sprintf('Position%02d.mat', k);
This will try to replace %02d with 2 digits. If k = 1 to 9, then it will append a zero.
5 comentarios
Ameer Hamza
el 18 de Oct. de 2020
Ture, sometimes all one needs is a correct hint, and the rest is easy to figure out.
Más respuestas (0)
Ver también
Categorías
Más información sobre Help and Support 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!