How to separate the field from a structure in MATLAB?

조회 수: 24 (최근 30일)
SUSHMA MB
SUSHMA MB 2016년 10월 3일
편집: Stephen23 2020년 11월 29일
I have a structure which is attached with this file named as 'shape.mat'. The structure contains the following fields: Geometry Boundary X Y...etc Now i want to separate X and Y field from the structure and store it in a cell named x_constraint and y_constraint respectively. Can it be done?
  댓글 수: 2
Stephen23
Stephen23 2016년 10월 3일
편집: Stephen23 2020년 11월 29일
@SUSHMA MB: You can access the data in the structure quite easily, why not do that?
Siva Priya Bollineni
Siva Priya Bollineni 2020년 9월 21일
편집: Siva Priya Bollineni 2020년 9월 21일
@Stephen Cobeldick, I have a matlab file in which i have a class with 4 properties out of which one is a structure with different fields in it. I need to call this matlab file from python through Matlab API engine.
1.Can you please let me know how to access those fields present in that structure of matlab file from python, (when runned the matlab file from python).
2. Also, is there anyway to access the uint64 type data of matlab in to python.
Please someone let me know about this, Thanks in advance.

댓글을 달려면 로그인하십시오.

채택된 답변

michio
michio 2016년 10월 4일
편집: michio 2016년 10월 4일
Your structure seems to be a nonscalar structure, so try using curly bracket:
load('shape.mat');
x_constraint = {S.X};
y_constraint = {S.Y};
  댓글 수: 4
SUSHMA MB
SUSHMA MB 2016년 10월 4일
How can i merge x_constraint and y_constraint into a single cell. I have tried
AllCell = [x_constraint;y_constraint];
But im getting the output as 2x3565cell. As 1x3565cell of x_constraint and y_constraint respectively. Can i get a new cell named 'AllCell' as 1x3565cell, within which the x_constraint and y_constraint are merged into 426X2 double, 64X2 double,.....etc. I hope i made it clear. Please refer the attached file
michio
michio 2016년 10월 4일
One way is to use arrayfun, which applies function to each element of array, in this case, function @(struct) [struct.X; struct.Y] is applied to the each element of a structure S.
load('shape.mat');
AllCell = arrayfun(@(struct) [struct.X; struct.Y], S, 'UniformOutput',false);

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Guillaume
Guillaume 2016년 10월 3일
Well, unless I completely misunderstood your question:
x_constraint = yourstructure.X;
y_constraint = yourstructure.Y;
However, there is no benefit to that. Why not use yourstructure.X whenever you want x_constraint and not bother creating x_constraint in the first place?
  댓글 수: 1
SUSHMA MB
SUSHMA MB 2016년 10월 4일
편집: SUSHMA MB 2016년 10월 4일
Thank you for the answer. But i tried this too, i am not able to extract the data as shown in the structure. Can you please look into the structure which i have attached with this file and tell me the solution. It gives only the value of first row only.

댓글을 달려면 로그인하십시오.


Dimas Rizky
Dimas Rizky 2020년 11월 29일
Just type :
x_constraint = shape.X(:,1);
y_constraint = shape.Y(:;1);
I hope it's working for you..

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by