Borrar filtros
Borrar filtros

Call vector by key

1 visualización (últimos 30 días)
Stefan M
Stefan M el 3 de Ag. de 2020
Respondida: Steven Lord el 4 de Ag. de 2020
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

Respuesta aceptada

Bruno Luong
Bruno Luong el 3 de Ag. de 2020
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 comentarios
Walter Roberson
Walter Roberson el 4 de Ag. de 2020
Stefan: make sure you use 'ValueType', 'any'
Stefan M
Stefan M el 4 de Ag. de 2020
Hi Bruno,
thanks for the clarification. I misread your example and did forget to pass 'ValueType', 'any'. Now it works
@Walter thank you, too

Iniciar sesión para comentar.

Más respuestas (1)

Steven Lord
Steven Lord el 4 de Ag. de 2020
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.

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by