Cell array as input function

I have a certain function that I need to use:
[t3_1_HR,q3_1_HR,qd3_1_HR,Tact3_1_HR,TSRS3_1_HR] = SimulatePendulumTest(m, kF, kdF, d, Tb, 1, tspanHR);
And I have a cell array with different tspanHR's (every row is a subject and each column is the number of HR files per subject). I want to know every outcome of the function for the different tspanHR values. How can I do this?
Thank you very much!

1 Kommentar

Rik
Rik am 12 Dez. 2020
This time I edited your question for you. Next time, please use the tools explained on this page to make your question more readable.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Ive J
Ive J am 12 Dez. 2020
Bearbeitet: Ive J am 12 Dez. 2020

0 Stimmen

You can use cellfun to apply your function to each cell.
[t3_1_HR, q3_1_HR, qd3_1_HR, Tact3_1_HR, TSRS3_1_HR] = ...
cellfun(@(x)SimulatePendulumTest(m, kf, kdF, d, Tb, 1, x), YourCellArray, 'uni', false);

4 Kommentare

Rik
Rik am 12 Dez. 2020
You do need to be carefull with the empty elements of tspanHR.
Ive J
Ive J am 12 Dez. 2020
Bearbeitet: Ive J am 12 Dez. 2020
% example
x = {1, 2, 3;4, 5, 6}; x{4} = [];
x =
2×3 cell array
{[1]} {[ 2]} {[3]}
{[4]} {0×0 double} {[6]}
foo = @(x, y) x + y;
outMe = cellfun(@(x, y)foo(x, 2), x, 'uni', false)
2×3 cell array
{[3]} {[ 4]} {[5]}
{[6]} {0×0 double} {[8]}
UniformOutput takes care of non scalar values of x.
It does, but your function should do that as well. cellfun does not automatically skip empty elements.
A=rand(2,3);
cellfun(@(x) size(A,x),{1,[]},'UniformOutput',false)
Error using size
Dimension argument must be a positive integer scalar or a vector of positive integers.

Error in solution (line 2)
cellfun(@(x) size(A,x),{1,[]},'UniformOutput',false)
Ive J
Ive J am 12 Dez. 2020
Oops! I see, you are absolutely right, I assumed user function in the simplest form.
A = rand(2, 3);
input = {1, []};
cellfun(@(x) size(A, x), input(~cellfun(@isempty, input)))

Melden Sie sich an, um zu kommentieren.

Kategorien

Gefragt:

am 12 Dez. 2020

Kommentiert:

am 12 Dez. 2020

Community Treasure Hunt

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

Start Hunting!

Translated by