How to pass a variable from one file to another?
4 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Ismaeel
il 20 Feb 2017
Commentato: Walter Roberson
il 22 Feb 2017
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]
%-----------------------------------------------------
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/160901/image.png)
%
3 Commenti
Risposta accettata
Walter Roberson
il 20 Feb 2017
Do not use global for that purpose.
7 Commenti
Walter Roberson
il 22 Feb 2017
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
%----------------------------------------------------
Più risposte (1)
Vedere anche
Categorie
Scopri di più su Discrete Events and Mode Charts in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!