How to insert a int value into the name of a variable (for loop)
Ältere Kommentare anzeigen
I'm trying to iterate throw these variable names in a for loop, here's my code:
form= 'dd-mmm-yyyy HH:MM:SS';
for i = 1:15
yearfrac=vpa(string([data_v3_data.b [INSERT i here] date]),10);
year = double(fix(yearfrac))
day = double(yearfrac-year)
a = datestr(datenum(year,1,1)+day*365)
data_v3_data.b + [INSERT i here] + date = datenum(a,form)
end
[INSERT i here] indicates where I would like the integer (1:15) to appear. I have a list of variables named:
data_v3_data.b1date
data_v3_data.b2date
data_v3_data.b3date
data_v3_data.b4date
etc etc..
Thanks !
2 Kommentare
Adam
am 1 Sep. 2017
Just use arrays and indexing. why use 15 variables when 1 will do?!
[INSERT i here]
tells us that you want an index. So why not use a real indexing instead of trying to write slow complicated code and pointlessly forcing meta-data (the index) into some names?
Indexing is fast and very efficient. Use it.
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Matrix Indexing finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!