How to store data in a for loop?

조회 수: 4 (최근 30일)
Chameleon17
Chameleon17 2015년 7월 17일
답변: Ghada Saleh 2015년 7월 20일
Hi,
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)
end
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

답변 (1개)

Ghada Saleh
Ghada Saleh 2015년 7월 20일
Hello,
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;
end
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,
Ghada

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by