How do I create a MATLAB function from some symbolic variables and a structure of symbolic variables?
4 ビュー (過去 30 日間)
古いコメントを表示
Hi everyone, and thanks in advance for the replies.
My code start like this:
x = {'X','Y','vx','vy'};
syms(x)
x=cell2sym(x);
assume(x,'real')
mystruct.a = sym('a',[1,2],'real');
mystruct.b = sym('b',[1,2],'real');
M = [x(1),x(2);mystruct.a(1),mystruct.b(1)];
What I want is to generate in the current folder a .m function file like this:
function M = myfunction(x,mystruct)
X = x(1);
Y = x(2);
a1 = mystruct.a(1);
b1 = mystruct.b(1);
M = [X,Y;a1,b1];
end
namely, in which its inputs are exactly (x,mystruct) and not (X,Y,a1,b1).
I tried to use the "matlabFunction" command in this way:
matlabFunction(M,'file','myfunction','Vars',{x,mystruct},'output',{'M'});
but it doesn't work because 'Vars' does not accept a struct (like mystruct). Does anyone knows how to overcome this problem or can propose me other approaches?
0 件のコメント
回答 (1 件)
Ananya Tewari
2021 年 3 月 22 日
I understand that you want your inputs to be (x,mystruct) for 'Vars' field matlabFunction().
The 'vars' field of matlabFunction() only accepts character vector, 1-d cell array of character vector or array of symbolic variables. Structres are not accepted by the 'Vars' field.
Here is a workaround for the above given code:
Rather than using structure we can use array of symbolic variables
x = {'X','Y','vx','vy'};
syms(x)
x=cell2sym(x);
assume(x,'real')
% creating two sym variables rather than structure
a = sym('a',[1,2],'real');
b = sym('b',[1,2],'real');
% creating array for the sym variables
y = [a(1) b(1)];
M = [x(1),x(2);y(1),y(2)];
matlabFunction(M,'file','myfunction','Vars',{x, y},'output',{'M'});
参考
カテゴリ
Help Center および File Exchange で Code Generation についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!