Using variables between m-files
3 次查看(过去 30 天)
显示 更早的评论
I want to use an input dialog box for inputs in one m-file and use these inputs in another function...
Here is the first code:
x = inputdlg({'Rate', 'Population renewal', 'Death rate'}, 'Inputs')
b = str2double(x{1});
p = str2double(x{2});
l = 0.5;
d = 0.9;
m = str2double(x{3});
v = (b*p)*(1+(l*d));
c = m*(m+l);
R = /c;
if (R<1)
answer = msgbox(['Value = ' num2str(R) ' therefore there is no epidemic'],'Basic Reproduction number')
else
answer = msgbox(['Value = ' num2str(R) ' therefore there is an epidemic'],'Basic Reproduction number')
end
---------------------------------------------------------------------------------------------
I want to use the same inputs in another function and I am using the following code:
function msir = msirtry(t,y)
load('salRo.m','l','p','b','m','d');
d = 0.9;
c = 0.14;
a = 0.25;
r = 0.05;
msir(1) = p - m*y(1)- b*y(1)*y(2)*(1+(l*d)) + a*y(4);
msir(2) = b*y(1)*y(2)*(1+(l*d)) - (m + l)*y(2);
msir(3) = l*d*y(2) - (m + r)*y(3);
msir(4) = l*c*y(2) + r*y(3) - (m + a)*y(4);
msir = msir(:);
--------------------------------------------
and i am running this function using
yo = [20 5 5 10];
[t y] = ode45(@msirtry,[0 5],yo);
plot(t,y(:,1),t,y(:,2),t,y(:,3),t,y(:,4),'Linewidth',2.5)
legend('Susceptible','Infected Symptomatic','Infected Asymptomatic','Recovered')
--------------------------------------------------------------
I get the error - Number of columns on line 1 of ASCII file
C:\Users\Krishnaa\Documents\MATLAB\salRo.m
must be the same as previous lines.
if i manually put in the inputs instead of loading it, the codes work but I need to work all this out together... pls help...
0 个评论
采纳的回答
dpb
2013-9-23
编辑:dpb
2013-9-24
Aircode -- untested!!! :)
This uses the nested function solution technique so that the variables in the function misfunc which is called from the script in model.m are visible inside the function used as the handle to ode45. This technique is described more in the documentation under first nested functions for them specifically then for their use in such situations w/ links from ode45
model.m
x = inputdlg({'Rate', 'Population renewal', 'Death rate'}, 'Inputs')
b = str2double(x{1});
p = str2double(x{2});
l = 0.5;
d = 0.9;
m = str2double(x{3});
v = (b*p)*(1+(l*d));
c = m*(m+l);
R = /c; % problem here...this intended to be 1/c, maybe???
yo = [20 5 5 10];
[t y] = msifunc,b,p,m,l,d,yo);
plot(t,y(:,1),t,y(:,2),t,y(:,3),t,y(:,4),'Linewidth',2.5)
legend('Susceptible','Infected Symptomatic','Infected Asymptomatic','Recovered')
% This seems peculiar -- the answer isn't anywhere a function of the
% other function and integral??? This is dependent entirely on the input
% constants...oh, well, ...
if (R<1)
answer = ...
else
answer = ...
end
msifunc.m
function [t,y]=msifunc(b,p,m,l,d,yo)
[t y] = ode45(@msirtry,[0 5],yo);
function msir = msirtry(t,y)
msir=zeros(4,1);
d = 0.9;
c = 0.14;
a = 0.25;
r = 0.05;
msir(1) = p - m*y(1)- b*y(1)*y(2)*(1+(l*d)) + a*y(4);
msir(2) = b*y(1)*y(2)*(1+(l*d)) - (m + l)*y(2);
msir(3) = l*d*y(2) - (m + r)*y(3);
msir(4) = l*c*y(2) + r*y(3) - (m + a)*y(4);
end
end
4 个评论
dpb
2013-9-24
编辑:dpb
2013-9-24
There is no method to pass variables other than passing the variables(+) which is what is done (other than the absolutely yucky choice of GLOBAL). Scope is scope and never the twain shall meet. If you're of age to remember think of programming in original BASIC without the concept and shudder... :)
The alternative way to write similarly-functioning code is to use anonymous functions instead of nested--this is also expounded upon in the previous link referenced. It can end up w/ somewhat shorter code but the actual implementation complexity is, imo, another step up from this approach which is why I chose it for your first exercise.
And, no, I didn't run it; I wrote it on the fly cutting and pasting your code--I lead off with the caveat of "air code"; it was simply to illustrate the structure of getting the variables from one scope to another; I make nor made no claim I didn't make a few typos or other mistakes; one being not knowing precisely what your objective really is as the code as given doesn't make a lot of sense in that it outputs things that appear totally independent of the integration...but it should suffice to get from a to b along with the documentation re: the question of passing the input variables to the target function of ode45, determining the actual functionality was left as "exercise for the student" :).
In the future if/when you have a specific problem with a solution (whether mine or someone else's), post it with sufficient detail to understand the issue instead of just the assertion it "doesn't work".
ADDENDUM: (+) There is one other way that is more like what you seemingly were attempting w/ the load of an m-file. save the variables from inside the one script that gets them and then load them in the routine that uses them. Of course, the top level script still has to call the other function and then one introduces the potential problems of keeping files in synch, timing issues, etc, etc., etc., ... So, that's not a very "clean" solution, either.
更多回答(2 个)
dpb
2013-9-23
You must either
a) make the variables GLOBAL so they're available in the other functions (HIGHLY not recommended), or
b) write an upper level script that calls the other functions in the proper, desired order that passes the needed inputs to the functions that need them (and then return the desired results for consumption/display).
You don't show what you did but it appears you tried to load an m-file instead of calling the function within it -- that, as you found out, doesn't work and isn't the way to use m-files/functions in Matlab.
Your last script should be turned into a function and passed the values you get from the dialog box or that few lines simply incorporated in the proper order in the script that calls the dialog box itself.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Install Products 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!