Call vector by key

5 次查看(过去 30 天)
Stefan M
Stefan M 2020-8-3
I would like to save some weather data in a container. The data consists of forecasts and actuall values for the years 2016-2018. To acces the data I want to use the years as a key, so when I call
windSpeedForecast(2016)
it will give me a vector with all hourly windSpeedForecasts of 2016, so a 8760x1 vector.
The default containers in Matlab are only able to save scalars as values and not vectors. Is there a way to achieve this or am I missing something in the containers.Map object?
Thanks in advance.
Best regards,
Stefan

采纳的回答

Bruno Luong
Bruno Luong 2020-8-3
Work for me
>> M=containers.Map('KeyType','double','valueType','any')
M =
Map with properties:
Count: 0
KeyType: double
ValueType: any
>> M(2020)=double('Covid')
M =
Map with properties:
Count: 1
KeyType: double
ValueType: any
>> char(M(2020))
ans =
'Covid'
>>
  4 个评论
Walter Roberson
Walter Roberson 2020-8-4
Stefan: make sure you use 'ValueType', 'any'
Stefan M
Stefan M 2020-8-4
Hi Bruno,
thanks for the clarification. I misread your example and did forget to pass 'ValueType', 'any'. Now it works
@Walter thank you, too

请先登录,再进行评论。

更多回答(1 个)

Steven Lord
Steven Lord 2020-8-4
Depending on what else you want to do with this data, storing it in a timetable may be useful. Using one of the examples on the documentation page for timetable:
MeasurementTime = datetime({'2015-12-18 08:03:05';'2015-12-18 10:03:17';'2015-12-18 12:03:13'});
Temp = [37.3;39.1;42.3];
Pressure = [30.1;30.03;29.9];
WindSpeed = [13.4;6.5;7.3];
TT = timetable(MeasurementTime,Temp,Pressure,WindSpeed)
Now let's retrieve all the rows with times between 9 AM and 1 PM on the date in question:
selectedTimes = timerange(datetime('2015-12-18 09:00:00'), datetime('2015-12-18 13:00:00'));
X = T(selectedTimes, :)
This particular example does not include any non-scalar data, but it is possible to store such data in a timetable variable.

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by