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

1 回表示 (過去 30 日間)
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!

採用された回答

Sachin Lodhi
Sachin Lodhi 2024 年 4 月 25 日
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!

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSimscape についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by