assignin a local variable to the base workspace

27 views (last 30 days)
Hi,I'm new to MATLAB. I need to calculate using ode15s, and I want to display a local variable (an array) in the base work space. I hope to get an array of this variable changing with t, but I keep getting one value.
Here is my main script:
dp0 = 2e-7;
Np0 = 1e11;
V0 = pi/6*dp0^3;
C_bulk = 1.42e21*V0*Np0;
c0 = [0;C_bulk];
options = odeset('RelTol',1e-10);
[t,c] = ode15s(@myfun,[0:120],c0,options,V0,C_bulk);
plot(t,40.9*4.065e-14*338*c(1),'r.',
t,40.9*4.065e-14*338*c(2),'b.');
The myfun.m file that includes the ode equations are below:
function dcdt = myfun(t,c,V0,C_bulk)
V = V0*c(2)/C_bulk;
dp = (6*V/pi)^(1/3);
dcdt(1) = 1.2e17*dp-c(1);
dcdt(2) = c(1)-1.2e17*dp;
assignin('base','V',V(:));
end
I would like to show the values of V, which is the local variable of myfun, in the base workspace. It is supposed to change with t, but I only get 1 value of V, not an array of 121 values. Please help me. Thank you.

Answers (2)

Image Analyst
Image Analyst on 14 May 2015
Don't do that. I've never had to use assignin() or evalin() or eval() ever . There is no reason to stick local variables into the base workspace. Simply put the name of the variable on its own line and it will print it out to the command window. Or else set a breakpoint in the function and look at it in the function's workspace where you can double click on it to bring it up in the Variable Editor if you want, where you'll be able to see more of it than you can in the workspace.

Walter Roberson
Walter Roberson on 14 May 2015
try
oldV = evalin('base','V');
catch:
oldV = [];
end
newV = [oldV, V];
assignin('base', 'V', newV);
The whole isn't recommended, but if you are going to do it anyhow then this is a way.
  4 Comments
H.O.Z.
H.O.Z. on 14 May 2015
Yes I made sure to initialize V. I am getting over 500 values. It seems the V in the local function has more iteration steps than the ode.
Walter Roberson
Walter Roberson on 14 May 2015
That's possible. You might want to use
try
oldtcV = evalin('base','tcV');
catch:
oldtcV = [];
end
newtcV = [oldtcV, [t;c(:);V]];
assignin('base', 'tcV', newtcV);
That would give you a record of every call, as a series of columns, t values in the first row, c values in the next rows, V value in the last row.

Sign in to comment.

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!