Unable to access all data in a Python DataArray

29 次查看(过去 30 天)
I have imported a Python DataArray into Matlab but I can't work out how to access all the data in the DataArray. My 1x1 DataArray is called st and when I run
>> st.size
ans =
Python int with properties:
denominator: [1×1 py.int]
imag: [1×1 py.int]
numerator: [1×1 py.int]
real: [1×1 py.int]
2038
So it says the size is 2038, which is as expected, but when I try to access the data (a row of alternating 'opened' 'closed' 'opened' ....), I only get a shortened version of the data. I want to get a full row of 2038 states (opened or closed; here I could of course generate that list myself but it is not always this predictable)
>> st.data.char
ans =
'['closed' 'opened' 'closed' ... 'opened' 'closed' 'opened']'
When I run
>> st.data
ans =
Python ndarray with properties:
T: [1×1 py.numpy.ndarray]
base: [1×1 py.numpy.ndarray]
ctypes: [1×1 py.numpy.core._internal._ctypes]
data: [1×2038 py.memoryview]
dtype: [1×1 py.numpy.dtype[object_]]
flags: [1×1 py.numpy.core.multiarray.flagsobj]
flat: [1×1 py.numpy.flatiter]
imag: [1×1 py.numpy.ndarray]
itemsize: [1×1 py.int]
nbytes: [1×1 py.int]
ndim: [1×1 py.int]
real: [1×1 py.numpy.ndarray]
shape: [1×1 py.tuple]
size: [1×1 py.int]
strides: [1×1 py.tuple]
['closed' 'opened' 'closed' ... 'opened' 'closed' 'opened']
I am hoping it is an easy problem to solve and that its just due to my very limited knowledge of Python.
Any help much appreciated
Jos

回答(1 个)

Gayatri Rathod
Gayatri Rathod 2023-5-24
Hi Joseph,
Based on the information you provided, it seems like you have imported a Python DataArray into MATLAB.
To access the full row of 2038 states (alternating 'opened' and 'closed'), you can try the following approach:
1. Access the st.data property, which gives you a Python ndarray" object.
2. Convert the Python ndarray object to a MATLAB cell array using the cellstr" function.
Example code snippet:
% Convert the Python ndarray to a MATLAB cell array
dataCellArray = cellstr(st.data);
% Access the full row of states
fullRow = dataCellArray{1};
% Display the full row
disp(fullRow);
The “cellstr" function converts each element of the Python ndarray to a cell array element. Then by accessing dataCellArray{1}", you retrieve the first element of the cell array which corresponds to the full row of states.
You can read more about the cellstr" function from the following documentation: cellstr function
Hope it helps!
  1 个评论
Joseph van 't Hoff
Joseph van 't Hoff 2023-6-12
thanks for your answer, in my case this does not work though, maybe because my ndarray is a numpy ndarray. When I run cellstr on it it gives
Error using cellstr
Conversion to cellstr from py.numpy.ndarray is not possible.
I found out how to convert it though, by using string(st.data.tolist) I get an array of strings with each cell containing 'opened' or 'closed'

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Call Python from MATLAB 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by