MATLAB: Evalin Loop Workspace fints

evalin loopFinancial Toolbox

Hello,
I am stuck on how to code in order to able to work with workspace variables.
I am struggling to include and indexing the evalin function in a loop. I used the who but its not really working.
I have 5 tables with different names in the workspace, they are of type fints.
I want to create a dataset which will merge/concatenate, one column of each five tables into one global array.
The fints(time series object) table have 5 columns, I want the close(3rd) column. To access it, u need to type TableName.Close.
I know this can be done with cell arrays, but here i am working with fints.
Thank you very much
Davin

Best Answer

Davin - have you tried stepping through the code to see what is happening? Let's suppose that you have a function called myFunc, and you have the five (fints) tables in the workspace. In the Command Window you type
myFunc
Now suppose the definition for this function uses your second code example
function myFunc
Global = []
a = who
for i = 1:length(a)
Data = evalin('base', [a{i}.Close])
Global = [Global; Data(:,2)]
end
The first line of code declares an empty array named Global. This should be renamed so as not to be confused with the built-in MATLAB global "qualifier" for global variables. Perhaps name it given the data that is being used to populate it: allCloseData. The next line calls the who function which, since we are evaluating it from a function, will return only those variables that have been declared in this function. I think that you want to grab the variables in the base workspace. So try
allBaseVars = evalin('base','who');
Now, you iterate over each one and grab the last (close) column. You mentioned that you are concerned with accessing variables that aren't of type fints - so just add a check on the class type
for k=1:length(allBaseVars)
classType = evalin('base',['class(' allBaseVars{k} ')']);
if strcmpi(classType,'fints')
% do stuff
end
end
I don't have the Financial Toolbox so I'm guessing that the class type of a Financial Time Series object is fints. You can verify this easily enough by just type class(myFintsTable) in the Command Window and seeing what is returned.
Now we can "do stuff" if the object is of type fints. The code will be similar to what you have shown, but the expression (the second input to evalin) must be a string and not the statement hat you have shown
data = evalin('base',[allBaseVars{k} '.Close']);
allCloseData = [allCloseData ; data];
So your function now becomes
function myFunc
allCloseData = [];
allBaseVars = evalin('base','who');
for k=1:length(allBaseVars)
classType = evalin('base',['class(' allBaseVars{k} ')']);
if strcmpi(classType,'fints')
data = evalin('base',[allBaseVars{k} '.Close']);
allCloseData = [allCloseData ; data];
end
end
If you want to assign the allCloseData to the base workspace, then just add the following line of code to your function
assignin('base', 'allCloseData', allCloseData);