Adding structs within structs in a larger scale (without having to manually specify every field name)
Afficher commentaires plus anciens
So, I'm not a very good or efficient coder, but I want to structure alot of data and i think structs within structs will be the easiest for me to work with. Say I want to add som specific structs to other struct fields in a larger struct, but have too many field names to work with to write them all manually with the cell2struct function, how would I do that? Can copy the full code I have for now, I'm happy for any input. The bottom for-loop is basically the relevant part, I want to add the struct TimeFrame to every Coin-field, and later on add the relevant data for each coin given each timeframe. (For now I do not load all data I want to have in the end, I'm working with a smaller subset for the time being)
Thanks!
%%Fix stuff
url = 'https://api.binance.com/api/v1/klines';
symbols = load('BinanceSymbols.mat');
symbols = struct2cell(symbols);
symbols2 = {1:10};
for i = 1:10
symbols2{i} = symbols{1,1}{i,1};
end
symbols2 = symbols2';
%intervals = {'1h','2h','4h','6h','8h','10h','11h','12h','13h','14h','15h','16h','17h','18h','19h','20h','21h','22h','23h','1d'};
intervals = {'4h','12h','1d'};
intervals = intervals';
data = zeros(length(symbols2),length(intervals));
data = num2cell(data);
%%For-loop for data
for i = 1:length(symbols2)
symbol = symbols2{i};
for k = 1:length(intervals)
interval = intervals{k};
data{i,k} = webread(url,'symbol',symbol,'interval',interval,'limit',25);
end
end
%%Build struct
length1 = length(data{1,1});
t = struct;
Date4 = struct;
Close4 = struct;
Open4 = struct;
High4 = struct;
Low4 = struct;
Data4 = cell2struct({Date4;Close4;Open4;High4;Low4},{'Date','Close','Open','High','Low'},1);
Date12 = struct;
Close12 = struct;
Open12 = struct;
High12 = struct;
Low12 = struct;
Data12 = cell2struct({Date4;Close4;Open4;High4;Low4},{'Date','Close','Open','High','Low'},1);
Date1d = struct;
Close1d = struct;
Open1d = struct;
High1d = struct;
Low1d = struct;
Data1d = cell2struct({Date4;Close4;Open4;High4;Low4},{'Date','Close','Open','High','Low'},1);
TimeFrame = cell2struct(intervals,{'FourHours','TwelveHours','OneDay'},1);
Coin = cell2struct(symbols2,symbols2,1);
for i = 1:length(data)
Coin(i).TimeFrame = TimeFrame;
end
The struct then looks like this:
Coin =
struct with fields:
ETHBTC: 'ETHBTC'
LTCBTC: 'LTCBTC'
BNBBTC: 'BNBBTC'
NEOBTC: 'NEOBTC'
BCCBTC: 'BCCBTC'
GASBTC: 'GASBTC'
BTCUSDT: 'BTCUSDT'
ETHUSDT: 'ETHUSDT'
HSRBTC: 'HSRBTC'
MCOBTC: 'MCOBTC'
TimeFrame: [1×1 struct]
And i want the TimeFrame to be inside every coin as another struct and not as another field under the coin!
Thanks again for any input.
Edit: Added binancesymbols.mat
2 commentaires
Stephen23
le 8 Mai 2018
@Erik Nyström: please upload 'BinanceSymbols.mat' by clicking on the paperclip button.
Erik Nyström
le 8 Mai 2018
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur MATLAB dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!