필터 지우기
필터 지우기

assigning a vector to struct array filedname

조회 수: 55 (최근 30일)
Payam Parsinejad
Payam Parsinejad 2012년 3월 9일
편집: Stephen23 2022년 8월 24일
Hi all,
I have a struct array holding multiple fileds, i.e.
objects(1). x = 6
objects(1). y = 4
objects(2). x = 6
objects(2). y = 4
objects(3).x = 2
objects(3). y = 1
In part of my code -just to make it easier for the calculation I used this command to select out all the values stored in filed name 'x' and store them in a vector X:
X = cell2mat({objects_init(:).x});
after modifying the values in vector X, I would like to update all the "x" fieldname of my objects struct array.
here's is my problem " how to do this?!". I mean given the fact that all the index in my vector are sorted, i would like to find a way -other than for loop- to iterate on the Vector X and assign each value stored in it, one by one (or all together by matrix calculation) to objects(index).x and so on.
for now I use for loop as the length of my X and update the objects(:).x. which I would like to avoid.
Thanks in advance if you could help me out in this.
Payam,

답변 (4개)

Jan
Jan 2012년 3월 9일
Why do you want to avoid the FOR loop? Do you assume, that a "vectorized" approach is faster? If so, will the gain of the runtime exceed the time, which is required for codeing - and posting this question?
Any solution in Matlab will use a loop. The only difference is, if you can see it directly, of if it is hidden in a subfunction. Even if you use a fast C-mex, there is still a loop.
My suggestion: Use a FOR loop. The time for searching a better version can be invested for more useful features like more tests or comments.
Btw. is this a more efficient option than the cell2mat call:
X = [objects_init(:).x];
?
  댓글 수: 2
Tamara del Águila
Tamara del Águila 2022년 8월 24일
...and what about the assignment in the other way?
[objects_init(:).x] = X
Why does this not work?
Stephen23
Stephen23 2022년 8월 24일
편집: Stephen23 2022년 8월 24일
"Why does this not work?"
Because you are trying to allocate one array on the RHS to multiple outputs on the LHS. The comma-separated list syntax on the LHS collects multiple arrays into the elements of that structure. But you only provide it with one array on the RHS, your code is equivalent to this:
[objects(1).x, objects(2).x, .., objects(end).x] = X;
which is not valid MATLAB syntax (when X is an array).
Depending on what you goal is, you could use DEAL or a comma-separated list on the RHS:
objects(1).x = 6;
objects(1).y = 4;
objects(2).x = 6;
objects(2).y = 4;
objects(3).x = 2;
objects(3).y = 1
objects = 1×3 struct array with fields:
x y
C = {7,8,9}; % data to add
[objects.z] = C{:}
objects = 1×3 struct array with fields:
x y z
objects.z
ans = 7
ans = 8
ans = 9

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


Koby Goldberg
Koby Goldberg 2018년 2월 15일
This should do the trick:
X = [1,3,5,2];
X_cell = num2cell(X);
objects.x = X_cell{:};

per isakson
per isakson 2012년 3월 9일
I guess this does what you ask for.
[objects.x] = deal_num( [X.x] )
function varargout = deal_num( vec )
varargout = num2cell( vec );
end
Sometimes deal_num makes the code more readable.
EDIT
>> X = [1,3,5,2];
mc(1,4) = cssm;
[mc.x]=deal_num(X);
mc(3).x
ans =
5
where cssm is
classdef cssm < handle
properties
x
end
end

Payam Parsinejad
Payam Parsinejad 2012년 3월 12일
Thanks Jan for the reply.
X = [objects_init(:).x];
was very helpful.
-----------------
Per Isakson: unfortunately your solution was not exactly assign the vector elements to the struct array field name. what it does is assign the whole vector to every sing objects.x!
let me tell you what I need:
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
  댓글 수: 1
per isakson
per isakson 2012년 3월 13일
It is a bit confusing to call a structure, objects. However, my solution works both with objects and structures. I added an example to my answere above.

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

카테고리

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