Clear Filters
Clear Filters

How can I call a directory that has a partially variable name (variable in the middle), that precedes a variable filename?

21 views (last 30 days)
I am having a hard time trying to put my problem into words, but I will try my best. To give some background, I have multiple files of data from tests that I ran that I need to process. Each test was performed on a different date, and to keep track of them all, I labeled their files according to their test number and date. I currently have a working code, where I ask the operator (which could be someone else who works in the lab who doesn't know MATLAB) to input the test number, date, and whether to temperature for the experiment was increasing or decreasing:
test = input('input the test number: ');
date = input('input the date of the test in YYYY-MM-DD format (no spaces): ','s');
p = input("Input 1 if the temperature was decreasing or 2 if the temperature was increasing: ");
After the operator inputs these variables (p is changed to a string labeled inc_dec in a for loop not shown because that part isn't important), I then look for the corresponding files from the appropriate folders, for example:
wv_filename = sprintf("/Directories/test_" + test + "_" + date + "_Data/test_" + test + "_" + date + "_Data_" + inc_dec + ".txt");
wv = readmatrix(wv_filename);
I have to open three other files that follow this same pattern and similar naming scheme for each test, but I just showed one to give an example and took out some of the indetifier words. The folder names cannot be changed since I have to follow a specific naming scheme.
While this works well for me right now, it requires the operator to know which date the test was performed on, which can get quite tedious since I sometimes need to plot a dozen or so plots at the same time sometimes. I will also eventually have to process data that automatically names the file based on the date and timestamp, and it would be very tedious to have to look for that for dozens of files. I read that MATLAB has a wildcard feature using the asterisk in a string to ignore certain parts of the filename, but I've only seen it used at the end of a filename and for a file within the directory the code is in, such as:
Is there a way to use this wildcard feature (or other feature) to replace where I include the date in the directory and in the filename, similar to how it can be used in the above example? I tried simply doing:
wv_filename = sprintf("/Directories/test_" + test + "_*_Data/test_" + test + "_*_Data_" + inc_dec + ".txt");
wv = readmatrix(wv_filename);
and that did not work. I also tried putting a backslash, "\", before the asterisk and that also did not work, giving me the error message:
"Error using readmatrix (line 157) Unable to find or open '/Directories/test_31_'. Check the path and filename or file permissions."
The directory definitely exists since it works without the asterisks, but I don't know how to get MATLAB to ignore the date and begin reading the directory and file name once it sees the word "Data", for example. Is there something I can type around the asterisks to make MATLAB skip over the string until it seems the next word I type, or is there any other function I might be able to use?

Accepted Answer

Geoff Hayes
Geoff Hayes on 6 Oct 2021
@nailah oliver - consider using dir to get a list of files that match your pattern. The result will be an array of structs that has the file name and path (to that file). You could then loop through that struct and read in each file. For example,
myFiles = dir(fullfile('/Users/*/Documents/*/*.pdf'));
for me returns an array with two elements:
>> myFiles(1)
ans =
struct with fields:
name: 'someFile1.pdf'
folder: '/Users/ghayes/Documents/MATLAB'
date: '30-Sep-2021 10:18:21'
bytes: 55557469
isdir: 0
datenum: 738429.429409722
>> myFiles(2)
ans =
struct with fields:
name: 'someFile2.pdf'
folder: '/Users/ghayes/Documents/other'
date: '23-Apr-2021 19:30:54'
bytes: 33791
isdir: 0
datenum: 738269.813125

More Answers (0)




Community Treasure Hunt

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

Start Hunting!