Azzera filtri
Azzera filtri

assigning a vector to struct array filedname

55 visualizzazioni (ultimi 30 giorni)
Payam Parsinejad
Payam Parsinejad il 9 Mar 2012
Modificato: Stephen23 il 24 Ago 2022
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,

Risposte (4)

Jan
Jan il 9 Mar 2012
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 Commenti
Tamara del Águila
Tamara del Águila il 24 Ago 2022
...and what about the assignment in the other way?
[objects_init(:).x] = X
Why does this not work?
Stephen23
Stephen23 il 24 Ago 2022
Modificato: Stephen23 il 24 Ago 2022
"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

Accedi per commentare.


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

per isakson
per isakson il 9 Mar 2012
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 il 12 Mar 2012
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 Commento
per isakson
per isakson il 13 Mar 2012
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.

Accedi per commentare.

Categorie

Scopri di più su Structures in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by