How do I search for and assign variables within my filespace?

2 views (last 30 days)
Hi guys, I'm working with mat files and I have a unique problem. I have to load in a certain format of mat file but after that I want to assign certain variables in the filespace. Problem is that switching from mat file to mat file yields slightly different variable names for the data I need.
For example, "data1.mat" may have "freq_hz_t0" as its frequency data but "data2.mat" may have "freq_hz_t1" as its frequency data. I would like to be able to extract this data from any mat file I have assuming that "freq_hz" is always present in the name although different strings may come after "freq_hz" is there a way to parse specifically for variables in the workspace that have certain keywords in the name?

Answers (2)

Image Analyst
Image Analyst on 29 Aug 2022
I see you didn't attach any files meaning you didn't read the posting guidelines. Here's another chance:
In the meantime, try this:
s = load(fileName)
allFieldNames = fieldnames(s)
There may be something in the Take it from there. If you need any more help, attach at least two mat files that have different field names.

Stephen23 on 29 Aug 2022
Once those differently-named variables are in the workspace then you are really up the creek without a paddle. Fiddling around with variable names using code is one way how beginners force themselves into writing slow, complex, inefficient, buggy, insecure, code. Best avoided:
Here are a few better ways to think about this problem. First lets create some fake data:
freq_hz_t0 = 1:3;
helloworld = 4:6;
save test freq_hz_t0 helloworld
Remember that you should always LOAD into an outut variable, to make your code robust and efficient. You can use LOAD's regular expression syntax to identify the required variable/s for you (much like you requested, but when loading, not once in the workspace):
S = load('test.mat','-regexp','freq_hz.*')
S = struct with fields:
freq_hz_t0: [1 2 3]
Each variable that you import will be one field of the structure S.
If you import exactly one variable into S, you can get that array without knowing its name:
C = struct2cell(S);
V = C{1}
V = 1×3
1 2 3
Another approach is to access the fieldnames of S:
S = load('test.mat')
S = struct with fields:
freq_hz_t0: [1 2 3] helloworld: [4 5 6]
F = fieldnames(S)
F = 2×1 cell array
{'freq_hz_t0'} {'helloworld'}
which clearly you can process using any text tools, e.g. STARTSWITH, etc:
X = startsWith(F,'freq_hz')
X = 2×1 logical array
1 0
and then easily access the fields using dynamic fieldnames:
V = S.(F{X})
V = 1×3
1 2 3
Note that I obtained the data array without using its specific name anywhere. You can too.
If all of the files used exactly the same variable names (and in particular did not hide meta-data in the variable names) you could avoid this complication entirely, allowing you to write simpler, more robust, more efficient code. Poor data design forces you into writing this complex code.




Community Treasure Hunt

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

Start Hunting!