assignin a local variable to the base workspace
Show older comments
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
on 14 May 2015
0 votes
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
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.
I'm pretty sure I pointed you to other resources: http://matlab.wikia.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.3F
4 Comments
H.O.Z.
on 14 May 2015
Walter Roberson
on 14 May 2015
What size do you get back for V?
Have you made sure to initialize V to [] in your code so that you are not getting values from previous runs?
H.O.Z.
on 14 May 2015
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.
Categories
Find more on Loops and Conditional Statements 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!