call different matlab function in for loop (SIMULINK)

Dear community,
I'm having trouble getting a function to work, that calls a different beta function depending on the interval.
The getBeta_l function is created in a Matlab script, and has a suffix depending of the specific curvenumber.
How do I call the function and link it to my simulink variable s (curvelength)
manually it would look like this:
if (0 <= s)&& (s<= R_kpi/2)
beta = getBeta_1(s);
elseif (R_kpi/2 < s)&& (s<= R_k3pi/2)
beta = getBeta_2(s);
elseif
.
.
.
.
now I'm trying to do this in a for loop, but I can't figure out, how to implement it properly...
if (0 <= s)&& (s<= R_kpi/2)
beta = getBeta_1(s);
else
for q = 2:nuc %nuc = number of curves
if (R_k(2q-3)pi/2 < s)&& (s<= R_kpi(2*q-1)/2) %Interval
filename=['getBeta_l',num2str(q)]; %get function
beta=filename(s); %call function (s)
end
end
end
Thanks for your help,
Johannes

Answers (2)

Hi Johannes,
You can use Matlab 'run' command to do this as shown below:
if (0 <= s)&& (s<= R_kpi/2)
beta = getBeta_1(s);
else
for q = 2:nuc %nuc = number of curves
if (R_k(2q-3)pi/2 < s)&& (s<= R_kpi(2*q-1)/2) %Interval
filename=['getBeta_',num2str(q)]; %get function
run([filename, '(', num2str(s), ')']); % run function
beta=ans; % assign ans to output variable
end
end
end
Best Regards,
Udara

5 Comments

Thanks for your answer,
sadly num2str(q) doesn't work because num2str only supports a constant.
I've tried sprintf, but that requires a constant aswell.
Any suggestions on that?
Hi Johannes,
I'm not sure what you meant by num2str only supports a constant. I have tried calling num2str in a for loop and it works fine. Also, I tried run([filename,'(',num2str(q),')']); which works for me. May be you can share the exact error message you are getting.
Regards,
Udara
Thanks for your help.
I think I wasn't explaining myself very well. I'm running this loop inside a Simulink MatLab function.
function beta = fcn(s,R_k, nuc)
if (0 <= s)&& (s<= R_k*pi/2)
beta = getBeta_1(s);
else
for q = 2:nuc %nuc = number of curves
if ((R_k*(2*q-3)*pi/2) < s)&& (s<= (R_k*pi*(2*q-1)/2)) %Interval
filename=['getBeta_',num2str(q)]; %get function
run([filename, '(', num2str(s), ')']); % run function
beta=ans; % assign ans to output variable
end
end
end
This is the Error I am getting:
Best regards
Johannes
It seems that the error is thrown by Matlab Coder. So I assume although 'run' works in matlab it may not work in simulink. Can you try 'feval' instead of 'run'?
It already fails before it gets to that line.

Sign in to comment.

discretize() to get an index number. Use it to index a cell array of function handles, and execute the resulting handle. No loops needed, and no if statements other than checking to be sure the input is within the bounds of discretization.

2 Comments

Thank you for your answer! I've looked through the documentation for discretize, but an an not sure how to implement it in my usecase.
I'm still very new to Matlab and programming.
It would be great if you could give me an example on how to use it in my usecase.
Thanks for your help!
Best regards
Johannes
Each time your q increases by 1, you move a "window" of width R_k further right and test to see if s is inside that window.
You could instead use
floor((s - SomeOffset)/Rk)
to directly calculate the appropriate q value. Discretization not needed.
You would have initialized
BH = {@getBeta_1, @getBeta_2, @getBeta_3 ... }
but first you would have put in an
assert(nuc <= SOME_LIMIT)
and then when you construct H, it will be either SOME_LIMIT or SOME_LIMIT+1 or SOME_LIMIT+2 entries (depending how you want to handle the boundary conditions on the two sides.) This assert() enforces that the index that gets used is no more than the maximum you configured for.
Once you have the index,
f = BH{q};
beta = f(appropriate_parameters);
Use real functions, not scripts for this purpose; Simulink cannot generate code for scripts.

Sign in to comment.

Categories

Products

Release

R2021b

Asked:

on 27 Jan 2022

Commented:

on 31 Jan 2022

Community Treasure Hunt

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

Start Hunting!