MATLAB Answers

Extract data from one array to another based on a string matching in Column 1

2 views (last 30 days)
Allan Munro
Allan Munro on 4 Dec 2020
Answered: Anmol Dhiman on 9 Dec 2020
I have this a program that collects the 'symbol', 'time' and 'VWAP' (which is a price, every thiry seconds. The data is collected for all the stocks in one array.
I am trying to get the data out for just one stock in the list and create a new array. I have tried ismember, sort, indexing and am getting errors on the string field for 'symbol'.
VWAP = []
TheList = {'ACB','AAPL'}
new_VWAPdata =[] % 1 run once
TheList = {'ACB','AAPL'}
% this data is rerun every 30 seconds to get the latest prices.
dataV = IQML('quotes', 'Symbol', TheList, 'Fields','Most Recent Trade Time,VWAP');
new_VWAPdata = [new_VWAPdata , dataV]
j = new_VWAPdata % renamed the array for ease of use in testing
I tried to sort the data and take out one symbol but got an error:
ind1 = j(:,1) == 'AAPL';
Operator '==' is not supported for operands of type 'struct'.
So I then converter the struct to an array:
C = struct2cell(j)
3×1×12 cell array
C(:,:,1) =
{'ACB' }
{'12:58:35.557713'}
{[ 11.7094]}
C(:,:,2) =
{'AAPL' }
{'12:58:35.412995'}
{[ 122.0862]}
C(:,:,3) =
{'ACB' }
{'12:59:16.777876'}
{[ 11.7097]}
etc...
C(:,:,12) =
{'AAPL' }
{'12:59:24.604849'}
{[ 122.0863]}
The I tried to get the data again from the new array and I get another error.
>> ind1 = C(:,1) == 'AAPL';
Operator '==' is not supported for operands of type 'cell'.
I have no idea where to go from here.
How do I create a new array with each variable on the arrays 'TheList' as a new array? Or extract the data for just 'AAPL' include Time and VWAP to a new array?
I've been stuck on this for hours.
Thanks
Allan (working on learning matlab during lockdown)
  2 Comments
Allan Munro
Allan Munro on 7 Dec 2020
Hi Anmol, here is the data I get from the array. I am trying to get the data out just for one symbol.
Thanks
Allan

Sign in to comment.

Answers (1)

Anmol Dhiman
Anmol Dhiman on 9 Dec 2020
Hi Allan,
It will be better if you convert your data into table with help of below command
T = struct2table(s); % convert the struct array to a table
sortedT = sortrows(T, 'Symbol'); % sort the table by 'Symbol'
sortedS = table2struct(sortedT);
You can also go through Table Operations in MATLAB in case you want to do more.
Hope it helps
Regards,
Anmol Dhiman

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!