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

3 views (last 30 days)
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

Accepted Answer

Rik
Rik on 16 Sep 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 Comments
Rik
Rik on 16 Sep 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.

Sign in to comment.

More Answers (0)

Categories

Find more on File Operations in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!