Unable to access all data in a Python DataArray

조회 수: 5 (최근 30일)
Joseph van 't Hoff
Joseph van 't Hoff 2022년 11월 23일
댓글: Joseph van 't Hoff 2023년 6월 12일
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