Retain existing column names when creating a timeseries object

4 views (last 30 days)
I'm only 2 days into using MatLab so please excuse the basic nature question. Also, please restrict the answer to using just MatLab, no addon packages. (I do have the Signal Processing & DSP packages but I'm not looking to spend more money as this is just an old retired EE playing at home.)
I have some stock price and indicator data saved from TradeStation in a general csv format:
mark@c2RAID6 ~/Builder/MatLab $ cat TLT-ex.csv
"Date","Time","Open","High","Low","Close","Vol","OI","Avg"
07/26/2002,16:00,82.67,82.80,82.42,82.51,316300,0,0.00
07/29/2002,16:00,82.06,82.16,81.32,81.42,8400,0,81.74
07/30/2002,16:00,81.75,81.90,81.52,81.52,6100,0,81.71
07/31/2002,16:00,81.95,82.80,81.90,82.53,29400,0,82.35
08/01/2002,16:00,82.54,83.02,82.54,83.00,25000,0,82.78
mark@c2RAID6 ~/Builder/MatLab $
Columns 1-8 are standard TS order. Column 9 (and higher if they exist) change from file to file.
I would like to convert this data to a timeseries object. (I think but that's debatable based on my limited knowledge of MatLab.) To do the conversion I wrote this code:
tmpData = readtable('~/Builder/MatLab/TLT-ex.csv');
Data = tmpData(:,3:size(tmpData, 2));
DataArray=table2array(Data);
DateCell = table2cell(tmpData(:,1));
TS1 = timeseries(DataArray, DateCell);
This seems to work - TS1 is created as a timeseries object - but there are two problems:
1) I'm not retaining the time column
2) I've lost the column names in the table2array step.
So, 3 questions:
1) How can I use the Time column when creating DateCell?
2) Most important, how do I retain the column names. In my general case there are many extra columns with different names which vary from file to file.
3) Once I get the names properly in the timeseries object (and the 'Data' element) how do I access a single column by name? I.e. - something like:
plot(TS1.Avg)
Thanks, Mark
  1 Comment
Mark Knecht
Mark Knecht on 3 Dec 2014
Edited: per isakson on 3 Dec 2014
Sort of answering myself. On StackOverflow I found reference to fieldnames, ala:
names = fieldnames(tmpData);
>> names(1:end-1)
ans =
'Date'
'Time'
'Open'
'High'
'Low'
'Close'
'Vol'
'OI'
'Avg'
>>
which appears usable if not a bit cumbersome in that I'll be having to figure out which column matches which position in this result all the time.
It appears that setfields doesn't work for arrays with numbers. Is that correct or can I somehow associate a specific name with a specific column without carrying around a bunch of list of strings?

Sign in to comment.

Accepted Answer

Mark Knecht
Mark Knecht on 4 Dec 2014
OK, I don't know if it's goof MatLab code - probably not as I'm not a programmer - but in case anyone else is interested in this at some future date here's how I solved the problem:
1) Created a function called ReadTradeStationData. It takes a file name as input, reads the file and then creates an array of timeseries objects, one for each column in the file. It sets the name of each ts to the name of the column it used.
2) Created a little program to call the function, print out the names, and then plot one of the ts objects.
function [ TS ] = ReadTradeStationData( FileName )
%ReadTradeStationData:
% Reads TradeStation csv file saved using
% DataWindow and returns timeseries
tmpData = readtable(FileName);
Data = tmpData(:,3:size(tmpData, 2));
names = fieldnames(Data);
DataArray=table2array(Data);
DateCell = table2cell(tmpData(:,1));
count = size(Data, 2)
for i=1:count
tmpStr = char(names(i));
% fprintf('%s\n', tmpStr);
TS(i) = timeseries(DataArray(:,i), DateCell, 'Name', tmpStr);
end
end
SymbolName = 'TLT';
FileName = '~/Builder/MatLab/TLT.csv'
TSx = ReadTradeStationData(FileName);
NumTS = size(TSx, 2);
for i = 1:NumTS
n1 = get(TSx(i), 'Name');
disp(sprintf('%s', n1 ));
clear n1;
end
tmpTS = TSx(i);
plot(tmpTS);

More Answers (0)

Community Treasure Hunt

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

Start Hunting!