sturctfun with two struct variable

2 visualizaciones (últimos 30 días)
yijun guo
yijun guo el 28 de Abr. de 2020
Comentada: Ameer Hamza el 28 de Abr. de 2020
Hello,
i would like to mutiply two struct: a and b (see below). Is it possible to use structfun like this:
structfun(@(x,y) x.*y,a,b,'UniformOutput',false)
a =
struct with fields:
T: [0 30 60 90 120]
speed: [24×5 double]
klemme30: [24×5 double]
voltage: [24×5 double]
vol_eff: [24×5 double]
q_meas: [24×5 double]
iccp: [24×5 double]
id: [24×5 double]
iq: [24×5 double]
elpower: [24×5 double]
b =
struct with fields:
T: [1 1 1 1 1]
speed: [24×5 logical]
klemme30: [24×5 logical]
voltage: [24×5 logical]
vol_eff: [24×5 logical]
q_meas: [24×5 logical]
iccp: [24×5 logical]
id: [24×5 logical]
iq: [24×5 logical]
elpower: [24×5 logical]

Respuesta aceptada

Ameer Hamza
Ameer Hamza el 28 de Abr. de 2020
Editada: Ameer Hamza el 28 de Abr. de 2020
You can write a function like this
function s3 = structMul(s1, s2)
names = fieldnames(s1);
for i=1:numel(names)
s3.(names{i}) = s1.(names{i}).*s2.(names{i});
end
end
Example
s1.a = [1 2 3];
s1.b = [5 2 1];
s2.a = [3 2 1];
s2.b = [1 8 4];
s3 = structMul(s1, s2);
>> s3 =
struct with fields:
a: [3 4 3]
b: [5 16 4]
  2 comentarios
yijun guo
yijun guo el 28 de Abr. de 2020
thank you for your help
Ameer Hamza
Ameer Hamza el 28 de Abr. de 2020
I am glad to be of help.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Structures en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by