How to use the values of an array as inputs for a function?
Show older comments
Hi there, I have this function:
function [ spectralLevels ] = getJEVesselSpectrum( vesselClass, frequency_Hz, speed_kts, ...
length_m, getDdecSPLs, includeStdDev)
...
and I have a cell array (10x6) with inputs (rows in the array), i.e, I need 10 values (each row one value).
How do I loop through the rows' values to be the inputs of the function?
Thank you in advance!
Accepted Answer
More Answers (1)
Assuming the function resturns a scalar ....
Will a simple loop suffice?
c = num2cell(rand(10,6));
y = zeros(size(c,1),1);
for ii = 1:numel(y)
y(ii) = getJEVesselSpectrum(c{ii,:});
end
y
Or w/o the loop
y = cellfun(@(x) getJEVesselSpectrum(x{:}),mat2cell(c,ones(size(c,1),1)))
function [ spectralLevels ] = getJEVesselSpectrum( vesselClass, frequency_Hz, speed_kts, ...
length_m, getDdecSPLs, includeStdDev)
% made up function
spectralLevels = sum([vesselClass,frequency_Hz, speed_kts, ...
length_m, getDdecSPLs, includeStdDev]);
end
4 Comments
Ana
on 5 Sep 2022
If the elements of the first column of the cell array are all string, either method will still work
% make up the input cell array
c = num2cell(rand(10,6));
c(1:5) = {"a"};
c(6:10) = {"b"};
% The code
y1 = zeros(size(c,1),1);
for ii = 1:numel(y1)
y1(ii) = getJEVesselSpectrum(c{ii,:});
end
y2 = cellfun(@(x) getJEVesselSpectrum(x{:}),mat2cell(c,ones(size(c,1),1)));
[y1 y2]
function [ spectralLevels ] = getJEVesselSpectrum( vesselClass, frequency_Hz, speed_kts, ...
length_m, getDdecSPLs, includeStdDev)
% made up function
if vesselClass == "a"
spectralLevels = sum([frequency_Hz, speed_kts, ...
length_m, getDdecSPLs, includeStdDev]);
else
spectralLevels = 5;
end
end
@Paul's function is, as he says, totally made up -- if the variables are of different types then an ordinary array of doubles doesn't work as the argument variable, true. That would require a cell array but maybe then that's an indication to rethink the data storage and consider using a table which can hold disparate data types and has a great deal of functionality built into it that might be useful in whatever is actually being done with said data; that part we "know nuthink!" about so don't have the benefit of that knowledge to add context.
A struct array might be another alternative, but would not be my first choice in all likelihood; they have some less-than-appealing ways needed to use them although there are cases they're ideal for, too.
In short, we don't have enough background information to be able to give unequivical answer; can only provide guidance/suggestions within general MATLAB coding best practices.
Paul
on 5 Sep 2022
As noted, we are just really guessing about what's contained in the elements of c and have just been illustrating methods assuming that all of the those elements are scalars. If, as noted as a possibility, the elements of c are all different types and sizes (typical use case for a cell array), the the mehod(s) shown here of generating a comma separated list from each row of c will still work.
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!