So, i'm a very beginner at Matlab and need some help in my task as i'm stuck here! Basically i have a lot of Matlab files which do the same functionality with different variables, i need to combine them in one Matlab file with one function and save these different values to iterate through them later and pass them to the function!
I've been searching which data type is suitable to hold the vars and i ended up with structs so my struct looks like:
W = struct('Band7', {7, 1099, 236, 260, 236, 260, 0},
'Band2', {2, 1078, 236, 300, 236, 300, 0},
'Band3', {3, 1829, 236, 100, 236, 100, 0},
'Band4', {4, 1367, 206, 500, 206, 500, 0},
'Band1', {1, 1123, 246, 170, 246, 170, 0}, ...);
My question is: how to loop through each band in the struct to pass it to the function like that -> RX_combined(W.Band4)
and how to loop through each band's value inside the function itself?!
According to Wolfie's answer i update my code to:
function main
% create a struct with the different vars
W = struct('Band7', {7, 1099, 236, 260, 236, 260, 0},
'Band2', {2, 1078, 236, 300, 236, 300, 0},
'Band3', {3, 1829, 236, 100, 236, 100, 0},
'Band4', {4, 1367, 206, 500, 206, 500, 0},
'Band1', {1, 1123, 246, 170, 246, 170, 0});
fields = fieldnames(W)
% iterate over all bands and pass them to the function.
for i=1:numel(fields)
fields(i)
RX_combined(W.(fields{i}))
end
end
my problem is how to access the values for every band inside the function?!
after some searching and working on the idea, i figured out that Matlab assumes that i'm distributing the cells of my array across the structure array elements rather than using every cell array as a unit which is what i wanted! referring to this answer .
To do that i fixed the problem by adding another curly braces to every cell array!
Now my code is:
function main
% create a struct with the different values of the RX bands to merge the files in one!
W = struct('Band7',{{7, 1099, 236, 260, 236, 260, 0}},'Band2',{{2, 1078, 236, 300, 236, 300, 0}},'Band3',{{3, 1829, 236, 100, 236, 100, 0}},'Band4',{{4, 1367, 206, 500, 206, 500,0}},'Band1',{{4, 1367, 206, 500, 206, 500,0}});
fields = fieldnames(W);
% iterate over all the bands and pass them to the function.
for i=1:numel(fields)
fields(i);
%Wait for the User's keypress : this allows us to run every RX band file one by one
a = input('Run the new RX file (y/n)? ','s')
if strcmpi(a,'y')
RX_combined( { W.(fields{i}) });
end
end
end
function [] = RX_combined(band)
P=int16([]);
numValues = numel(band);
for i = 1: numValues
P.Band= band{i}{1};
P.Channel_Frequency= band{i}{2};
P.RD_GAIN_1= band{i}{3};
P.RD_GAIN_ANA= band{i}{4};
P.RX_GAIN_1= band{i}{5};
P.RX_GAIN_ANA= band{i}{6};
P.RX_ULP= band{i}{7};
end
disp (P);
end