Unable to access all data in a Python DataArray
조회 수: 5 (최근 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
댓글 수: 0
답변 (1개)
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.
Hope it helps!
참고 항목
카테고리
Help Center 및 File Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!