How to store data in a for loop?

9 views (last 30 days)
Chameleon17 on 17 Jul 2015
Answered: Ghada Saleh on 20 Jul 2015
I'm certain this is quite a basic and simple problem I'm having, but I've can't seem to figure it out.
I have data I'm processing for a series of years, for 652 locations for each year.
I made storage space at the start of my script so that I can store a date in each of the 652 locations at the end.
The script is done so that I am pulling out the dates I need for each year.
for Date = 1:652
Location(Date) = day_of_year(Date);
Real_Date = datestr(Location)
Result(Date) = Real_Date(Date)
P_Year(year,:) = Result
This is my latest attempt to to organize and store my data.
Location is my storage vector for the 652 locations, day_of_year is my results I want to store for each year.
It just doesn't seem to work. I would like to be able to store the results for each year in 652 sized vector and save each year separately. I might just be going round in circles and I know this is probably a very simple fix.
Any advice or direction to a video or something to read even would be very much appreciated.
Thank you

Answers (1)

Ghada Saleh
Ghada Saleh on 20 Jul 2015
I understand that you want to convert the dates stored in 'day_of_year' to its string format and then store it in 'result'. In that case, you can do the following:
for Date = 1:652
Location = day_of_year(Date);
Real_Date = datestr(Location);
Result{Date} = Real_Date;
P_Year{year,:} = Result;
Note that 'Real_Date' is an array of strings (each character is stored as a separate element). So in order to copy each date as a single element in 'Result', you will need to define 'Result' as a cell array. To access the data in a cell array , you can use curly parenthesis.
I hope this helps,

Community Treasure Hunt

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

Start Hunting!