Evalin Loop Workspace fints

3 次查看(过去 30 天)
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
  1 个评论
Davin
Davin 2014-10-7
编辑:Davin 2014-10-7
I have tried to following but not really working,
Global = []
a = who
for i = 1:length(a)
Data = a(i).Close
Global = [Global; Data(:,3)]
end
This one too :
Global = []
a = who
for i = 1:length(a)
Data = evalin('base', [a{i}.Close])
Global = [Global; Data(:,2)]
end
I think there are 2 issues, iterating on who, is not great, because it creates an intermediary array, so on second execution, it will count intermediary tables. Also the fints table.
Any suggestions?

请先登录,再进行评论。

采纳的回答

Geoff Hayes
Geoff Hayes 2014-10-7
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);
  2 个评论
Davin
Davin 2014-10-7
Thanks once more Geoff. In the mean time, I succeeded to resolve it with something a bit like you but with some more line of codes and without the strcmpi. I used a fts2mat to put it in a double array and then a horzcat to make the global array. Your version is much more optimised.. Thanks again.
Davin
Davin 2014-10-7
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 ; fts2mat(data)];
end
end
Apparently, the concatenate does not work with fints, it requires a double, so i added a fts2mat to transform the data array. Now it works, I removed purposedly the date in the array.
May be there is a better way without passing by fts2mat...
cheers

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by