Extract from Structure and make a table

12 vues (au cours des 30 derniers jours)
Kushal Kharel
Kushal Kharel le 25 Déc 2020
Commenté : Image Analyst le 25 Déc 2020
Hello Everyone,
I downloaded 55 stocks data from yahoo finance for past 5 years.
mydata = hist_stock_data(now-5*365,now,'T','VZ','GOOG','NFLX','FB','EBAY','SBUX','TSLA','NKE','AMZN','KO','CL','PEP','PG','WMT','XOM','EOG','KMI','CVX','WMB','MS','JPM','WFC','BAC','GS','CVS','PFE','JNJ','UNH','ANTM','BA','CAT','HON','FDX','NOC','MSFT','CSCO','MU','ADBE','NVDA','SHW','FMC','MOS','CF','LYB','CBRE','SPG','KIM','BXP','AVB','FE','NI','PPL','AEP','CMS')
The data is stored as 1x55 structure format containing field names Date(1259x1 cell [Open,High,Low,Close,AdjClose,Volume,Ticker](1259x1 double)
I want to extract fields Date,AdjClose and concatenate all three in one table. How do i do it?. Also need to extract Ticker names and replace AdjClose with ticker names
Basically from the structure, I want to extract fields Date and AdjClose for all 55 stocks and make a table and replace AdjClose with ticker names. Any help is greatly appreciated. Thank you

Réponses (1)

Image Analyst
Image Analyst le 25 Déc 2020
Modifié(e) : Image Analyst le 25 Déc 2020
See attached demo where I do the same thing - read in Yahoo financial data that I downloaded from their web site.
Or use this function:
Description
T = struct2table(S) converts the structure array, S, to a table, T. Each field of S becomes a variable in T.
T = struct2table(S,Name,Value) creates a table from a structure array, S, with additional options specified by one or more Name,Value pair arguments.
  3 commentaires
Kushal Kharel
Kushal Kharel le 25 Déc 2020
%mydata = hist_stock_data(now-5*365,now,'T','VZ','GOOG','NFLX','FB','EBAY','SBUX','TSLA','NKE','AMZN','KO','CL','PEP','PG','WMT','XOM','EOG','KMI','CVX','WMB','MS','JPM','WFC','BAC','GS','CVS','PFE','JNJ','UNH','ANTM','BA','CAT','HON','FDX','NOC','MSFT','CSCO','MU','ADBE','NVDA','SHW','FMC','MOS','CF','LYB','CBRE','SPG','KIM','BXP','AVB','FE','NI','PPL','AEP','CMS')
T = struct2table(mydata)
StockTicker = T.Ticker'
Date = T.Date
AdjClose = T.AdjClose
Output for Date is
55×1 cell array
{1259×1 cell}
{1259×1 cell}
{1259×1 cell}
{1259×1 cell}
{1259×1 cell}
{1259×1 cell}
{1259×1 cell}...
Output for StockTicker is all the stock names in row format
Output for AdjClose is
55×1 cell array
{1259×1 double}
{1259×1 double}
{1259×1 double}
{1259×1 double}
{1259×1 double}
{1259×1 double}
{1259×1 double}
{1259×1 double}...
What I want to do now is extract the values inside this 55x1 cell array for both Date and Adj close and assign the values to stock names in StockTicker and make a table. Output should look like
Date T VZ GOOG NFLX
12/21/2020 1 2 3 4
12/22/2020 5 6 7 8
Image Analyst
Image Analyst le 25 Déc 2020
I don't know - we'd have to play around with it like you've been doing. I can't just imagine trying code and debugging it in my head for something this complicated. You'd have to attach your table in a .mat file or else the original data files and code for reading them in, then someone can try something. But it would be something like getting each value into a column vector then use table():
table(dates, T, VZ, Goog, Nflx, 'VariableNames', {'Date', 'T', 'GOOG', 'NFLX'});

Connectez-vous pour commenter.

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by