How can I create structure entries in a for loop.?

I have multiple data sets. For each data set, I would like to perform multiple calculations and have the results of those calculations be entries in a structure. For instance, say I have three data sets for which I'd like to calculate the maximum and minimum values and create entries in a fields in a structure for each maximum and minimum. I'm currently doing these calculations for each data set separately without a for loop, and my code is repetitive. I think I should be able to do this, but I haven't been able to figure out how. I think my code should look something like this:
data = [data1 data2 data3]
for j = length(data)
s.data(j)_max = max(data(j))
s.data(j)_min = min(data(j))
end
I don't know how to create the fields - s.data(j)_max and s.data(j)_min - of the the structure s using the entires in the array data. Can someone please help me?

 Réponse acceptée

Stephen23
Stephen23 le 8 Jan 2022
Modifié(e) : Stephen23 le 10 Jan 2022
Using a cell array and a structure array:
C = {data1,data2,data3};
for k = 1:numel(C)
S(k).max = max(C{k});
S(k).min = min(C{k});
end
Or one structure array (more robust, recommended):
S = struct('data',{data1,data2,data3});
for k = 1:numel(S)
S(k).max = max(S(k).data);
S(k).min = min(S(k).data);
end

7 commentaires

Thank you for the response!
I'd like to name the new fields in the structure using the entries of "data" ([data1 data2 data3]). I need to have the maximum and minimum of each data set as part of the same structure s, so the new fields need to be s.data1_max, s.data1.min, s.data2_max, s.data2_min, s.data3_max, and s.data3_min. How would I do that?
Thank you!
Stephen23
Stephen23 le 10 Jan 2022
Modifié(e) : Stephen23 le 12 Jan 2022
"I need to have the maximum and minimum of each data set as part of the same structure s..."
Which is exactly what my answer does, by efficiently using indexing and just two fieldnames.
"... so the new fields need to be s.data1_max, s.data1.min, s.data2_max, s.data2_min, s.data3_max, and s.data3_min."
Why do you need to force pseudo-indices into the fieldnames? Given that using actual indices (like I used in my answer) is much simpler and more efficient, what is your specific use-case that requires you to write such obfuscated code? Sigh... oh well, if you really want to make your code more complex for no obvious benefit, you can use SPRINTF (or similar) and dynamic fieldnames:
For example:
C = {data1,data2,data3};
S = struct();
for k = 1:numel(C)
S.(sprintf('complicated_max_%d',k)) = max(C{k});
S.(sprintf('complicated_min_%d',k)) = min(C{k});
end
I know which code I would use (hint: the simpler and more efficieint approach using indexing).
Thank you for the update.
I tried your first suggestion with some made up data.
data1 = [1 3 2 5 6 2.5 10 9 8.1 4.4];
data2 = [2 6 4 10 12 5 20 18 16.2 8.8];
data3 = [1.1 3.3 2.2 5.5 6.6 2.75 11 9.9 8.91 4.84];
C = {data1,data2,data3};
for k = numel(C)
S(k).max = max(C{k});
S(k).min = min(C{k});
end
The result is the following 1x3 structure S
[] []
[] []
1.10000000000000 11
Why am I only getting entries for the third dataset? Is the problem caused by my made up datasets being arrays?
"Why am I only getting entries for the third dataset?"
Because I forgot to specify the loop iterator correctly. I have fixed the answer, it should be like this:
for k = 1:numel(C)
% ^^ I forgot this
Is the problem caused by my made up datasets being arrays?"
No. The corrected code works fine:
data1 = [1 3 2 5 6 2.5 10 9 8.1 4.4];
data2 = [2 6 4 10 12 5 20 18 16.2 8.8];
data3 = [1.1 3.3 2.2 5.5 6.6 2.75 11 9.9 8.91 4.84];
C = {data1,data2,data3};
for k = 1:numel(C)
% ^^ oops!
S(k).max = max(C{k});
S(k).min = min(C{k});
end
S(1).max
ans = 10
S(1).min
ans = 1
S(2).max
ans = 20
S(2).min
ans = 2
S(3).max
ans = 11
S(3).min
ans = 1.1000
Thank you, Stephen.
Your updated code works fine.
Your obfuscated code also works with a slight modification. Here is the version of that code that worked for me:
data1 = [1 3 2 5 6 2.5 10 9 8.1 4.4];
data2 = [2 6 4 10 12 5 20 18 16.2 8.8];
data3 = [1.1 3.3 2.2 5.5 6.6 2.75 11 9.9 8.91 4.84];
C = {data1,data2,data3};
S = struct();
for k = 1:numel(C)
S.(sprintf('%s_max',num2str(k))) = max(C{k});
S.(sprintf('%s_min',num2str(k))) = min(C{k}); end
It is simpler to use the %d format specifier, rather making things more complex by adding slow NUM2STR:
sprintf('%d_max',k)
% ^ ^
"Your obfuscated code also works with a slight modification."
Structure fieldnames must start with a letter, so your modification will not work:
S = struct();
S.(sprintf('%d_max',2)) = pi
Invalid field name: '2_max'.
Using the fieldnames that I showed you however does work, as they have leading letters.
Thank you for the suggestion about num2str being slow. I replaced num2str with your suggestion of using %d, and the script is much faster.

Connectez-vous pour commenter.

Plus de réponses (0)

Produits

Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by