필터 지우기
필터 지우기

How to add a whole field at once (if possible)?

조회 수: 2 (최근 30일)
Rim A.
Rim A. 2017년 10월 29일
답변: Rim A. 2017년 10월 29일
Here is a shortened version of the code:
%stars in the Orion constellation
etoile(1).nom='Betelgeuse';
etoile(1).dist_al=490;
etoile(1).magnitude=0.44;
etoile(2).nom='Bellatrix';
etoile(2).dist_al=250;
etoile(2).magnitude=1.64;
etoile(3)=struct('nom','Saiph','dist_al',650,'magnitude',2.06);
%Add a field (luminosity compared to the sun)
etoile(1).luminosite_nxsoleil=120000;
etoile(2).luminosite_nxsoleil=10000;
etoile(3).luminosite_nxsoleil=50000;
Is there any way to do the last three lines (creating a new field containing 12000 10000 and 50000) in one operation?
Thanks!

채택된 답변

Walter Roberson
Walter Roberson 2017년 10월 29일
lums = [12000 10000 50000];
%I will presume that the luminosities are being read in somehow, rather than
%being hard coded.
lumsc = num2cell(lums);
[etoile.luminosite_nxsoleil] = deal(lumsc{:});
If you were hard coding then
lums = {12000 10000 50000};
[etoile.luminosite_nxsoleil] = deal(lums{:});

추가 답변 (2개)

KL
KL 2017년 10월 29일
[etoile(1:3).luminosite_nxsoleil] = deal(120000,10000,50000)

Rim A.
Rim A. 2017년 10월 29일
Thanks! The deal function is exactly what I was looking for!

카테고리

Help CenterFile Exchange에서 Gravitation, Cosmology & Astrophysics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by