Get special variable out of every struct in workspace

38 vues (au cours des 30 derniers jours)
Henning Schmitz
Henning Schmitz le 26 Jan 2023
Modifié(e) : Stephen23 le 26 Jan 2023
Hello together,
i want to extract a certain variable out of the structural array from every variable which is loaded into the workspace. I got a .mat file which is containing a huge list of 1x1 structs ("a", "b", ...) . Inside each of these structs there are three variables (time (double), signals (1x1 struct) and name (char)). I am only interested in the signals. These signals struct is also containing data called "values" (double).
Is there any way to extract the "values" out of these 2 structs for each variable which is loaded into the workspace?
Later on i would like to have them back in my workspace called again as "a", "b", ... but then as a double variable containing the "values".

Réponses (2)

Jan
Jan le 26 Jan 2023
This is a bad coding design. Creating a bunch of variables causes much troubles. See here for an exhaustive discussion: https://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval
Loading variables directly into a workspace is a bad idea also. This is equivalent to eval'ing them, such that the debugging is massively impeded and the JIT acceleration cannot work efficiently anymore. This can increase the run-time of the code by a factor of 100.
Prefer clean programming techniques and catch the output of load() in a variable:
data = load('yourMatFile.mat');
dataField = fieldnames(data);
nField = numel(dataField);
Signal = cell(1, nField);
for k = 1:nField
Signal{k} = data.(dataField{k}).signal.value;
end
Now Signal{k} is the field 'value' of the k.th field of the data. This does not depend on the number of fields, while using a bunch of variables 'a', 'b', ... is fragile: What is coming after 'z'? How can you check later, how many signals have been imported? Using a cell array as the above 'Signal', this is trivial.

Stephen23
Stephen23 le 26 Jan 2023
Modifié(e) : Stephen23 le 26 Jan 2023
"Is there any way to extract the "values" out of these 2 structs for each variable which is loaded into the workspace?"
Yes, but only if you want to write slow, complex, inefficient code that is buggy and hard to debug:
"I got a .mat file which is containing a huge list of 1x1 structs ("a", "b", ...)"
Then you should LOAD that MAT file into one variable (which itself is a scalar structure):
S = load(..);
The fields correspond to the variables in the MAT file. Processing one variable is much easier than your slow and complex data design with lots and lots of separate variables.
"Inside each of these structs there are three variables (time (double), signals (1x1 struct) and name (char))."
C = struct2cell(S);
T = [C{:}]; % 1xN structure with fields TIME, SIGNALS, and NAME.
"Is there any way to extract the "values" out of these 2 structs for each variable which is loaded into the workspace?"
Once we LOAD into one variable (not like your complex approach) then this is easy. The exact steps will depend on the sizes of the arrays, which you have ot told us. But for example, you could vertically concatenate them:
Q = [T.signals]; % 1xN structure
V = vertcat(Q.values) % operation depends on sizes and desired output
but because you did not give any explanation of their sizes of how you want them joined, I will not guess more than that. Read more about how it works:
"Later on i would like to have them back in my workspace called again as "a", "b", ... but then as a double variable containing the "values"."
Ugh. Do not do that. Learn to use vectors, matrices and arrays. The name MATLAB comes from "MATrix LABoratory", not from "lets have lots of separate variables in the workspace and make our code slow and complex".

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by