Creating a structure using doubles from within other structures

I am loading in 2 .mat files that are both structures, however need to use values from doubles within the structure to create a new structure called data. Where each double is 15365 x 1 double.
load LEJ3.mat %(1x1 struct)
load LEJ3WAVE.mat %(1x1 struct)
Create a structure for the data:
data.time = struct(LEJ3WAVE.data.wave_height.time); %data time from LEJ3 from January 1, 2021 to January 1, 2022 (datetime in 30 minute intervals)
data.wavehgt1 = struct(LEJ3WAVE.data.wave_height.values); %wave height values (meters)
data.waveper1 = struct(LEJ3WAVE.data.wave_period.values); %wave period values (seconds)
data.wavedir1 = struct(LEJ3WAVE.data.wave_direction.values); %wave direction (degrees)
data.windspd1 = struct(LEJ3.data.wind_speed.values); %wind speed (meters/second)
data.winddir1 = struct(LEJ3.data.wind_from_direction.values); %wind direction (degrees North)
I keep receiving:
Error using struct. Conversion to struct from double is not possible.
Am I supposed to break down the double into an array or is what I'm trying to accomplish simply not feasible?

 Réponse acceptée

data.wavehgt1 = struct(LEJ3WAVE.data.wave_height.values); %wave height values (meters)
There are three possibilities in that statement.
  1. LEJ3WAVE or LEJ3WAVE.data might be non-scalar structs; in that case, further indexing would cause an error (but the error message would be different than the one you report)
  2. LEJ3WAVE.data.wave_height might be a scalar struct; in that case, LEJ3WAVE.data.wave_height.values would be a single entity, and struct() would be a request to convert that entity to a structure. The kinds of entities that can be converted to struct include some kinds of object-oriented variables, including most graphics objects, and including things like transfer functions. If you had a single entity that was not something that could be converted to a structure, then you would get an error message. In particular if LEJ3WAVE.data.wave_height.values is a double precision array, then it cannot be converted to struct and you would get exactly the error message you report
  3. LEJ3WAVE.data.wave_height might be a non-scalar struct; in that case, LEJ3WAVE.data.wave_height.values would undergo struct expansion, and you would be passing multiple parameters to the struct() call. This is potentially legal, provided that the odd-numbered entries are character vectors or scalar string() objects, each that happens to be a variable name, and the result would be to construct a new struct with the odd-numbered entries as the field names and the even-numbered entries as the field contents. If there were an odd number of parameters or the odd numbered ones were not valid variable names, you would get error messages different than the ones you observed.
Thus most likely is that LEJ3WAVE.data.wave_height.values is double precision. It is not clear what you want to do with them. Perhaps what you are looking for is
data.time = LEJ3WAVE.data.wave_height.time; %data time from LEJ3 from January 1, 2021 to January 1, 2022 (datetime in 30 minute intervals)
data.wavehgt1 = LEJ3WAVE.data.wave_height.values; %wave height values (meters)
data.waveper1 = LEJ3WAVE.data.wave_period.values; %wave period values (seconds)
data.wavedir1 = LEJ3WAVE.data.wave_direction.values; %wave direction (degrees)
data.windspd1 = LEJ3.data.wind_speed.values; %wind speed (meters/second)
data.winddir1 = LEJ3.data.wind_from_direction.values; %wind direction (degrees North)

1 commentaire

Yep, exactly what I was looking for. I was trying to overcomplicate creating a 1x1 structure with each of those. Thank you for your expertise.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Produits

Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by