Hi all, I have a matlab function block in Simulink with a feedback. The input to the function is a constant that changes with the time. The function calls another function includes ode15s (File1) that must get their equations from another file (File2). I want to pass the value of B from the main matlab function file to File2 to be used there. I used global but that did not work. Any idea?
Matlab Function File:
function y = fcn(B)
...
y=File1
%-----------------------------
File 1
function [t,x]=File1
....
[t,x] = ode15s(@File2,tspan,x0,opt);
%-----------------------------------
File2
function out= File2(t,x)
...
out=[some equations that use "B" as a constant]
%-----------------------------------------------------
%

3 件のコメント

Easwar Kumar Yarrabikki
Easwar Kumar Yarrabikki 2017 年 2 月 20 日
When you say It Didn't work, How was the error looks like...?
Ismaeel
Ismaeel 2017 年 2 月 20 日
"The GLOBAL declaration must precede any use of the variable B."
Ismaeel
Ismaeel 2017 年 2 月 20 日
"Global declaration not resolved to a Data Store Memory block registered via the Ports and Data Manager."

サインインしてコメントする。

 採用された回答

Walter Roberson
Walter Roberson 2017 年 2 月 20 日

1 投票

7 件のコメント

Ismaeel
Ismaeel 2017 年 2 月 21 日
Thanks Walter, I read the document but still I have the same problem. I was wondering how there is no direct way to get the port value of the block and share it with other files?
Walter Roberson
Walter Roberson 2017 年 2 月 21 日
"I was wondering how there is no direct way to get the port value of the block and share it with other files"
You can route the signal that is output from Subsystem2 to multiple blocks; each of them can receive B on input. Likewise you could route the output of the MATLAB Function Block to multiple blocks.
However, File1 and File2 are not blocks, they are MATLAB functions. You cannot route signals to or from them.
function y = fcn(B)
...
y = File1(B)
%-----------------------------
function [t,x] = File1(B)
....
[t,x] = ode15s(@(t,x) File2(t,x,B), tspan, x0, opt);
%-----------------------------------
function out = File2(t, x, B)
...
out=[some equations that use "B" as a constant]
%----------------------------------------------------
Not a global in sight.
Ismaeel
Ismaeel 2017 年 2 月 21 日
I see now, thanks for the clarification.
Ismaeel
Ismaeel 2017 年 2 月 21 日
編集済み: Ismaeel 2017 年 2 月 21 日
Sorry Walter, I have followed the nested method mentioned in the link you provided but unfortunately, it is not supported using matlab function block.
Walter Roberson
Walter Roberson 2017 年 2 月 21 日
Hmmm, as indicated in one of your other Questions recently, that should be supported provided that the function handle is buried like is shown. What error message are you receiving?
Ismaeel
Ismaeel 2017 年 2 月 21 日
I am trying to upload a picture of the error but there is something prevents it after uploading it. Anyway, this is what i got: Nested functions are not supported.
Function 'MF' (#83.127.128), line 8, column 15: "f" Launch diagnostic report.
Walter Roberson
Walter Roberson 2017 年 2 月 22 日
If you have multiple functions in the same file, make sure each is terminated with "end" as soon as it is finished.
function y = fcn(B)
...
y = File1(B)
end
%-----------------------------
function [t,x] = File1(B)
....
[t,x] = ode15s(@(t,x) File2(t,x,B), tspan, x0, opt);
end
%-----------------------------------
function out = File2(t, x, B)
...
out=[some equations that use "B" as a constant]
end
%----------------------------------------------------

サインインしてコメントする。

その他の回答 (1 件)

Ismaeel
Ismaeel 2017 年 2 月 20 日

0 投票

Also, I added a block "To Workspace" with the name "B" but still B is undefined.

カテゴリ

ヘルプ センター および File Exchange で Programming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by