How do I search for and assign variables within my filespace?
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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?
1 comentario
Stephen23
el 29 de Ag. de 2022
"...after that I want to assign certain variables in the filespace"
Do you mean workspace, where variables are stored/accessible from?
Respuestas (2)
Image Analyst
el 29 de Ag. de 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.
0 comentarios
Stephen23
el 29 de Ag. de 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
clearvars
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.*')
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}
Another approach is to access the fieldnames of S:
S = load('test.mat')
F = fieldnames(S)
which clearly you can process using any text tools, e.g. STARTSWITH, etc:
X = startsWith(F,'freq_hz')
and then easily access the fields using dynamic fieldnames:
V = S.(F{X})
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.
0 comentarios
Ver también
Categorías
Más información sobre Cell Arrays en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!