Save struct indexed name as a variable

15 visualizzazioni (ultimi 30 giorni)
This may sound Weird, Yet I need to save a struct index string as a variable. reason:The struct is formed as a query request for stock values - and in order to draw a valid graph the date is needed - the Date is the name of the struct Struct:
% code
end
stock.TimeSeries_Daily_
% Within
stock.TimeSeries_Daily_
% the struct continues as:
stock.TimeSeries_Daily_.x2017_12_01,
stock.TimeSeries_Daily_.x2017_11_30,
stock.TimeSeries_Daily_.x2017_11_29,
stock.TimeSeries_Daily_.x2017_11_28,
stock.TimeSeries_Daily_.x2017_11_27
and so on
Within final struct I get the needed value
Yet I need a variable to hold:
if true
% code
end
2017_11_30
2017_11_29
2017_11_28
and so on..
meaning I need to save the "name" of the struct into a different varible
How do I do this??
Thank you in Advance
  2 Commenti
Stephen23
Stephen23 il 4 Dic 2017
This is rather awkward. Just use indexing, and store the date as data in its own right:
stock.TimeSeries_Daily(1).date = ...
stock.TimeSeries_Daily(2).date = ...
Or put the dates into one array. Why do you need to put the meta-data into the fieldnames?
Daniel Grebler
Daniel Grebler il 4 Dic 2017
The Data is sent that way from webread function. The date represent an open market day so I cant "choose" my own dates (due to historical events - holiday weekends and crises)

Accedi per commentare.

Risposta accettata

Jos (10584)
Jos (10584) il 3 Dic 2017
You can use FIELDNAMES:
FN = fieldnames(stock.TimeSeries_Daily_)
% process these names in more common date formats using datenum and datestr
num = datenum(FN,'xyyyy_mm_dd')
str = datestr(num,'yyyymmdd')

Più risposte (1)

Steven Lord
Steven Lord il 4 Dic 2017
Are you required to store your data in this format? If you're using release R2016b or later I recommend using a timetable array instead. If you do store your data in a timetable named TT, you can access the list of times using:
TT.Properties.RowTimes

Tag

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by