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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 File Operations 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by