How to use private functions lib on ThingSpeak's matlab visualization like local Matlab program?

1 view (last 30 days)
Hi all,
I would like to save my several functions once and use them in all my visualizations (as we would do it in every local Matlab program). However, I didn't find any way to do this on Thingspeak. My workaround is to copy+paste all my functions and parameters in every visualization code. This works, but it is not nice.
For example:
main program (source m-file):
% Simple test model to check compatiblity with ThingSpeak
I=[0 0 1 1 5 5 2 2 1 1]; % Assume this random signal simulates the cell current (input) in 10 seconds
for i=1:10
x=I(i);
V_ECM(i)=test_func(x,i); % solve the cell model ODE
end
plot(V_ECM) % plot to see if the model is executed correctly
function program (function m-file):
function y=test_func(x,i)
syms Vc(t)
ode=diff(Vc)+2*Vc==2*x; % Assume this is one diff equation in cell model
cond=Vc(0)==0; % assign initial conditions
Vcsol(t)=dsolve(ode,cond); % solve ODE with the given initial condition
y=Vcsol(i); % sent back calculation at current time stamp i to the main M-file
end
If I want to execute this example in one file on ThingSpeak visualization, it shows following error:
Program:
I=[0 0 1 1 5 5 2 2 1 1]; % Assume this random signal simulates the cell current (input) in 10 seconds
% Simple test model to check compatiblity with ThingSpeak
for i=1:10
x=I(i);
V_ECM(i)=test_func(x,i); % solve the cell model ODE
end
plot(V_ECM) % plot to see if the model is executed correctly
function y=test_func(x,i)
syms Vc(t)
ode=diff(Vc)+2*Vc==2*x; % Assume this is one diff equation in cell model
cond=Vc(0)==0; % assign initial conditions
Vcsol(t)=dsolve(ode,cond); % solve ODE with the given initial condition
y=Vcsol(i); % sent back calculation at current time stamp i to the main M-file
end
Error:
syms requires Symbolic Math Toolbox.
Error in Test>test_func (line 14)
syms Vc(t)
Error in Test (line 7)
V_ECM(i)=test_func(x,i); % solve the cell model ODE
Could anyone help on how can I call functions several time on ThingSpeak visualization like we do in Desktop Matlab program?
Thanks!

Accepted Answer

Sachin Lodhi
Sachin Lodhi on 25 Apr 2024
Hi Tanusree,
I understand that you are encountering an error while using syms on ThingSpeak's MATLAB Visualization. As a workaround for this, you can use the 'matlabFunction' to convert symbolic expression to a function handle and then you can use the function handle without Symbolic Math Toolbox.
Here is how you can achieve this -
syms test_func(x, i)
syms Vc(t)
ode=diff(Vc)+2*Vc==2*x; % Assume this is one diff equation in cell model
cond=Vc(0)==0; % assign initial conditions
Vcsol(t)=dsolve(ode,cond);
tF(x,i) = Vcsol(i);
ht = matlabFunction(tF)
ht = function_handle with value:
@(x,i)x-x.*exp(i.*-2.0)
Now you can update the 'test_func' according to the handle 'ht' generated as shown below, to run it on ThingSpeak.
I=[0 0 1 1 5 5 2 2 1 1]; % Assume this random signal simulates the cell current (input) in 10 seconds
% Simple test model to check compatiblity with ThingSpeak
for i=1:10
x=I(i);
V_ECM(i)=test_func(x,i); % solve the cell model ODE
end
plot(V_ECM) % plot to see if the model is executed correctly
function y = test_func(x,i)
y = x-x.*exp(i.*-2.0); % update according to the function handle
end
I recommend you to please go through the following documentation for more information on 'matlabFunction' : https://www.mathworks.com/help/symbolic/sym.matlabfunction.html
I hope this helps!

More Answers (0)

Categories

Find more on Simscape 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!