How to generate function handle automatically from the cell array?

I have a 2x1 cell called "dx", which contains 1x3 cells:
'X * k * m' '*' '-1'
'X * k * m' '*' 'Y_XS'
I want this "dx" cell to be converted into a function. How can I do that? The generated function should look like this:
function dX = myFunc(t,X,k,m,Y_XS)
R = X(2) * k * X(1)/(X(1)+m);
dX = [-R;
Y_XS * R];

댓글 수: 5

Stephen23
Stephen23 2018년 4월 12일
편집: Stephen23 2018년 4월 12일
Is it strictly required to write a file, or would it be enough to generate a function handle? Writing files is slow.
"How can I do that without usind the symbolic Toolbox?"
Why would you need to use the symbolic toolbox? You have a cell array of char vectors: all you need to do is open a text file, write those char vectors in the order of your choice using fprintf, and close the file when you are done.
File Generation is not strictly required, that was just my first idea. Generating a function handle sounds better.
Stephen23
Stephen23 2018년 4월 13일
편집: Stephen23 2018년 4월 13일
@Akbar Shadakov: It would help if you gave a bit more information:
  1. Does the cell array always have the same size?
  2. Does the second column always contain binary operators?
  3. Can the third column contain other variables?
  4. Is the cell (1,3) always a constant?
  5. Where are x, y and z specified? (are they symbolic?)
Akbar
Akbar 2018년 4월 13일
편집: Akbar 2018년 4월 13일
Thank You very much for helping me. I'm developing a script, at the Moment the cell Array that i have is the very first Version, it might change later.
1. No, the size is not always same.
2. Lets assume yes, for simplicity.
3. Yes
4. No
5. Please check this, I have edited the question. Basically I'm trying to create ODE function. x is a variable and others are constants which are available on workspace. I have nothing of type symbolic.
Akbar
Akbar 2018년 4월 13일
편집: Akbar 2018년 4월 13일
FYI: I'm reading simulink model which simulates ODE. The Task is to get the Parameters of ODE and create a function inside matlab. So Im getting those Parameters into the cell Array called "dx". Now I want to create a function somehow.

댓글을 달려면 로그인하십시오.

 채택된 답변

Stephen23
Stephen23 2018년 4월 13일
편집: Stephen23 2018년 4월 13일
@ashadako: you have since edited your question and completely changed the function specification. This answer give the original specification: you can make the required changes yourself.
The trick is to simply arrange the strings as required, and then use str2func to get a function handle:
dx = {...
'X * k * m' '*' '-1'
'X * k * m' '*' 'Y_XS'};
isn = isnan(str2double(dx(:,3)));
tmp = dx;
tmp(:,4) = {';'};
tmp = tmp(:,4:-1:1).';
str = [tmp{:}];
var = sprintf(',%s',dx{isn,3});
str = sprintf('@(t,X,k,m,%s)[%s];',var(2:end),str(2:end));
fun = str2func(str);
This gives an anonymous function, which you can simply call with the required inputs:
>> fun(1,2,3,4,5,6)
ans =
-24
120

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기

질문:

2018년 4월 12일

댓글:

2018년 4월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by