Prev: Concatenated code simulation(Simulink)
Next: can Matlab save an auxiliary function computed during an ODE solution?
From: Frank on 28 Feb 2010 16:27 Hi all! I need your help. My problem is the following: I have a function in which a certain number (equal to the a priori unknown length of the input vector) of output variables are created. Now the problem... At the end of the function execution the local variables are disposed by default and I cannot find them in the workspace. I have tried with persistent, global... I do not want to use varargout to create a structure of output variables, I just would like to have them separately in the workspace as they are created. This is because I then need them in other routines that use them separately. I really thank you very much in advance! Frank
From: Steven Lord on 28 Feb 2010 22:55
"Frank " <francesco.manni01(a)fastwebnet.it> wrote in message news:hmen39$qvj$1(a)fred.mathworks.com... > Hi all! > > I need your help. My problem is the following: > > I have a function in which a certain number (equal to the a priori unknown > length of the input vector) of output variables are created. Now the > problem... At the end of the function execution the local variables are > disposed by default and I cannot find them in the workspace. Correct -- each function has its own workspace, and once a function ends its workspace goes away. http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/f7-38085.html > I have tried with persistent, global... I do not want to use varargout to > create a structure of output variables, I just would like to have them > separately in the workspace as they are created. This is because I then > need them in other routines that use them separately. Then use VARARGOUT to create a _cell array_ (not structure) in your function. When you do this, you can call your function with the desired number of outputs and MATLAB will assign the contents of the appropriate cell from VARARGOUT to the appropriate output. http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/bresuxt-1.html#br67dei-1 function varargout = myfunction % Call this as [a, b, c] = myfunction % and the variables will contain: % a = 1, b = 2, c = 3 varargout{1} = 1; varargout{2} = 2; varargout{3} = 3; % or varargout = {1, 2, 3}; If you're creating a LARGE number of varaibles this way, to keep the calling signature relatively compact, instead consider packing together your output variables into fields of a struct (I know you said you didn't want that, but you should consider it.) function S = myfunction % Call this as mystruct = myfunction % and mystruct.a will be 1, mystruct.b will be 2 % and mystruct.b will be 3. S.a = 1; S.b = 2; S.c = 3; -- Steve Lord slord(a)mathworks.com comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ |