Skip to content

Commit

Permalink
Continuing Fix: Python to MATLAB string array conversion
Browse files Browse the repository at this point in the history
This code attempts to create a MATLAB double from a numpy array

MATLAB versions older that 2022a are throwing this error:

```
Error ID:
      ---------
      'MATLAB:invalidConversion'
      --------------
      Error Details:
      --------------
      Error using double
      Conversion to double from py.list is not possible.
```
  • Loading branch information
simmsa committed Mar 6, 2024
1 parent 64bff1f commit 2ac4c21
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions mhkit/utils/convert_numeric_dataframe_to_struct.m
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
function result_struct = convert_numeric_dataframe_to_struct(df)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Returns
%
% Converts a numeric pandas DataFrame to a MATLAB struct.
%
% Parameters
% ------------
% delft_3d_py_object: py.netCDF4._netCDF4.Dataset
% A netCDF python object.
% seconds_run: number
% Time index
% df: DataFrame
% A numeric pandas DataFrame.
%
% Returns
% ---------
% result: number
% The closest time index
% result_struct: struct
% A MATLAB struct containing the data from the DataFrame.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Guard to check type of df
if ~isa(df, 'py.pandas.core.frame.DataFrame')
error('MATLAB:convert_numeric_dataframe_to_struct:InvalidInput', 'df must be a pandas DataFrame.');
end

columns = cell(df.columns.values.tolist());
result_struct = struct();

for i = 1:length(columns)
this_column = char(columns{i});
result_struct.(this_column) = double(df.get(this_column).values.tolist());
result_struct.(this_column) = double(py.numpy.array(df.get(this_column).values));
end
end

0 comments on commit 2ac4c21

Please sign in to comment.