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

3 ビュー (過去 30 日間)
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

採用された回答

Rik
Rik 2022 年 9 月 16 日
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 件のコメント
Rik
Rik 2022 年 9 月 16 日
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.
Luis Eduardo Cofré Lizama
Luis Eduardo Cofré Lizama 2022 年 9 月 18 日
Many thanks Rik! working!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by