reading files that contain changing parts and parts that need to be ignored

2 visualizzazioni (ultimi 30 giorni)
Hi there I am trying to read in a loop a series of files that have certain parts that need to be ignored and others vary. For example I want to read the file 'h001s1_EC1_Sway.h5' for which I did the following considering the varying parts of the file:
read(strcat(saveres,'***',num2str(r),'_s',num2str(s),'_',cond,num2str(t),'_****.h5'));
but didnt work.
Any ideas how can I do it so I ignore the '*' parts when reading the file?
Many thanks

Risposta accettata

Rik
Rik il 16 Set 2022
You need to use dir to find the actual file name matching some pattern. Then you can provide the function with the real file name. Unless explicitly stated otherwise, Matlab functions do not allow you to provide a file pattern instead of a real name.
  4 Commenti
Rik
Rik il 16 Set 2022
Did you read the documentation for the dir function? I suspect you didn't. A wildcard in the context of dir can replace multiple characters, so just 1 asterisk will suffice. You also need to provide a single char array.
Something like this should do.
ListOfFiles = =dir(strcat(...
saveres,'*',num2str(r),'_s',num2str(s),'_',cond,num2str(t),'_*.h5' ));
That will return a struct with file details. If you're lucky there will only be 1 match and you can use this to extract the file name:
filename = ListOfFiles.name;
If there are multiple matches, you need to loop through the result to find the one you need. The regexp function may be helpful in that case.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su File Operations in Help Center e File Exchange

Prodotti


Release

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by