Clear Filters
Clear Filters

How to call a symbolic function from cell array

7 views (last 30 days)
Hello, i have a problem with calling a symbolic function that is stored in cell array.
syms pp(x);
>> pp(x) = piecewise(x<0 , 0, 0<=x<=0.9 ,0.8, .9 <x<=2.7 ,0.9, 2.7<x<=4.5, 0.8, 4.5 <x<=8.1 , 0.4, 8.1 <x<=13.5 , 0.05);
>>
>> PP=cell(10,1);
>> PP{1}=pp(x);
>> pp(3)
ans =
4/5
>> PP{1}(3)
Index exceeds the number of array elements (1).
Error in sym/subsref (line 902)
R_tilde = builtin('subsref',L_tilde,Idx);
>>
Someone can help me understand how i have to call it? Thank you very much !

Answers (1)

Harimurali
Harimurali on 13 Oct 2023
Hi Annette,
I understand that you want to know if there is a way to call the symbolic function stored in a cell array. In this case, calling this symbolic function from the cell array is not possible.
Alternatively, the same can be achieved by creating a MATLAB function handle for the ‘piecewise’ function and storing it in the cell array. This can be called directly by indexing the cell array. The sample MATLAB code for this is as follows:
pp = @(x, ~, ~, ~, ~, ~, ~, ~, ~, ~, ~, ~)piecewise(x < 0, 0, 0 <= x <= 0.9, 0.8, 0.9 < x <= 2.7, 0.9, 2.7 < x <= 4.5, 0.8, 4.5 < x <= 8.1, 0.4, 8.1 < x <= 13.5, 0.05);
PP = cell(10, 1);
PP{1} = pp;
PP{1}(3);
Please refer to the following documentation for more information about function handles in MATLAB:
I hope this helps.

Categories

Find more on Symbolic Math Toolbox in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!