# Multiple loops to build structure?

4 views (last 30 days)
Joe on 18 Feb 2024
Commented: Stephen23 on 19 Feb 2024
I have k=6 matrices of sound data that I am trying to loop thru to create structures for plotting.
Each matrix has 9 channels: X1-X3, Y1-Y3, Z1-Z3. Each channel has 5 columns of data. The first column is time, and the next 4 columns are sound data: a hi and lo set of + and data.
I want to create 6 structures that contain data from the 6 groups with some manual inputs for naming.
The structures need to have a group, name, hi or lo, positive or negative, and then the data that gets read in from columns.
Can someone show me a faster and easier way of doing what I’m doing this? Especially because it is so repetitive. Below is my futile approach to setting this up with an example of how I plan to plot it.
%group matrix = k=1
g(1).x1.group = 'Group 1:';
g(1).x1.name = 'North';
g(1).x1.ch1.name.pos = 'Channel Hi';
g(1).x1.ch1.name.neg = 'Channel Lo';
g(1).x1.ch1.pos = [group(1)(:,1),group(1)(:,2)];
g(1).x1.ch1.neg = [group(1)(:,1),group(1)(:,3)];
g(1).x1.ch2.name.pos = 'Channel Hi';
g(1).x1.ch2.name.neg = 'Channel Lo';
g(1).x1.ch2.pos = [group(1)(:,1),group(1)(:,4)];
g(1).x1.ch2.neg = [group(1)(:,1),group(1)(:,5)];
%
%Group 2 X data
g(1).x2.group = 'Group 1:';
g(1).x2.name = 'East';
g(1).x2.ch1.name.pos = 'Channel Hi';
g(1).x2.ch1.name.neg = 'Channel Lo';
g(1).x2.ch1.pos = [group(1)(:,1),group(1)(:,6)];
g(1).x2.ch1.neg = [group(1)(:,1),group(1)(:,7)];
g(1).x2.ch2.name.pos = 'Channel Hi';
g(1).x2.ch2.name.neg = 'Channel Lo';
g(1).x2.ch2.pos = [group(1)(:,1),group(1)(:,8)];
g(1).x2.ch2.neg = [group(1)(:,1),group(1)(:,9)];
%Group 3 X data
g(1).x3.group = 'Group 1:';
g(1).x3.name = 'West';
g(1).x3.ch1.name.pos = 'Channel Hi';
g(1).x3.ch1.name.neg = 'Channel Lo';
g(1).x3.ch1.pos = [group(1)(:,1),group(1)(:,10)];
g(1).x3.ch1.neg = [group(1)(:,1),group(1)(:,11)];
g(1).x3.ch2.name.pos = 'Channel Hi';
g(1).x3.ch2.name.neg = 'Channel Lo';
g(1).x3.ch2.pos = [group(1)(:,1),group(1)(:,12)];
g(1).x3.ch2.neg = [group(1)(:,1),group(1)(:,13)];
%group 2
g(2).x1.group = 'Group 2:';
g(2).x1.name = 'North';
g(2).x1.ch1.name.pos = 'Channel Hi';
g(2).x1.ch1.name.neg = 'Channel Lo';
g(21).x1.ch1.pos = [group(2)(:,1),group(2)(:,2)];
.
.
.
%and so on, and so on
figure()
for k = 1:4
legStr = sprintf( '%s', g(k).x1.ch1.name.pos);
loglog(g(k).x1.ch1.pos(:,1), g(k).x1.ch1.pos(:,2), 'k-', 'Displayname', legStr); hold on; grid minor;
legStr = sprintf( '%s', g(k).x1.ch2.name.neg);
loglog(g(k).x1.ch2.neg(:,1), g(k).x2.ch2.neg(:,2), 'r-', 'Displayname', legStr);
end
VBBV on 19 Feb 2024
Edited: VBBV on 19 Feb 2024
May be you can extend the code which you plotted at the end by using additional for loop as shown below
for k = 1:6 % for groups
for J = 1:3 % for channels
g(k).x(J).group = 'Group 1:';
g(k).x(J).ch1.name.pos = 'Channel Hi'
g(k).x(J).ch1.name.neg = 'Channel Lo';
g(k).x(J).name = 'North';
end
end
Stephen23 on 19 Feb 2024
"Can someone show me a faster and easier way of doing what I’m doing this? Especially because it is so repetitive."
Use indexing.
Avoid putting meta-data (e.g. pseudo-indices) into the fieldnames (or names of variables).
Computers are really only very good at repeating very simple tasks in loops. So when you sit and copy-and-paste lots of lines of code like that you are just doing the computer's job for it. Much better: use indexing, let the computer do the looping.

Sulaymon Eshkabilov on 19 Feb 2024
It can be done somewhat what like this one:
GN = {'Group 1:'; 'Group 2:'; 'Group 3:'};
NA = {'North'; 'East'; 'West'];
for ii = 1:3
g(ii).x1.group = GN{ii};
g(ii).x2.group = GN{ii};
g(ii).x3.group = GN{ii};
g(ii).x1.name = NA{ii};
g(ii).x2.name = NA{ii};
g(ii).x3.name = NA{ii};
...
end