Call vector by key

5 ビュー (過去 30 日間)
Stefan M
Stefan M 2020 年 8 月 3 日
回答済み: Steven Lord 2020 年 8 月 4 日
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.

カテゴリ

Help Center および File ExchangeWeb Services についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by