MATLAB Answers

Retain existing column names when creating a timeseries object

6 views (last 30 days)
Mark Knecht
Mark Knecht on 3 Dec 2014
Answered: Mark Knecht on 4 Dec 2014
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
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));
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:
Thanks, Mark
  1 Comment
Mark Knecht
Mark Knecht on 3 Dec 2014
Sort of answering myself. On StackOverflow I found reference to fieldnames, ala:
names = fieldnames(tmpData);
>> names(1:end-1)
ans =
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 )
% Reads TradeStation csv file saved using
% DataWindow and returns timeseries
tmpData = readtable(FileName);
Data = tmpData(:,3:size(tmpData, 2));
names = fieldnames(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);
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;
tmpTS = TSx(i);

More Answers (0)

Community Treasure Hunt

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

Start Hunting!