필터 지우기
필터 지우기

converting a struct array fildname to an array

조회 수: 1 (최근 30일)
Payam Parsinejad
Payam Parsinejad 2012년 3월 14일
Hi all,
I'm having a struct array of arrays in which I store an array to one of its fieldnames:
o(1).color = [100, 256, 60]
o(2).color = [100, 100, 0]
o(3).color = [100, 0, 60]
and I want to extract all the values in "color" fieldname into a an array forming:
COLOR = [[100, 256, 60] ; [100, 100, 0] ; 100, 0, 60]]
the problem is when I use:
COLOR = [o(:).color];
I get:
COLOR = [100, 256, 60, 100, 100, 0, 100, 0, 60]
which is not what I want. any solution?
Thanks,

답변 (2개)

Thomas
Thomas 2012년 3월 14일
col = [o(:).color];
new_color=reshape(col,3,[])'
This should help
  댓글 수: 2
Payam Parsinejad
Payam Parsinejad 2012년 3월 14일
Thanks Thomas; that does it :)
Thomas
Thomas 2012년 3월 14일
Please consider accepting the answer so that others know that a solution has been obtained..

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


Payam Parsinejad
Payam Parsinejad 2012년 3월 14일
another sort of related question:
I have a vector, X, holding some values:
X = [1,3,5,2] Also I have a struct array, objects:
objects(1).x =#
objects(2).x =#
objects(3).x =#
objects(4).x =#
what I want to do is to assign all the values (or some) in X to their corresponding index number in objects.x. i.e.
X -> objects.x
so that I have: objects(1).x =1 objects(2).x =3 objects(3).x =5 objects(4).x =2
any solution?
Thanks, Payam
  댓글 수: 2
Thomas
Thomas 2012년 3월 14일
X = [1,3,5,2]
for i=1:length(X)
object(i).x=X(i);
end
Payam Parsinejad
Payam Parsinejad 2012년 3월 14일
Hi Thomas,
Can I do such process without using for loop?!
Thanks,
Payam

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

카테고리

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