필터 지우기
필터 지우기

Changing field value for all fields in a struct

조회 수: 30 (최근 30일)
MiauMiau
MiauMiau 2014년 4월 3일
댓글: deejt 2021년 1월 19일
Hi
Given for instance I have a struct as follows:
s(2) = struct('name','anna','age',18)
s(1) = struct('name','bernd','age',22)
Now I want to change all the names to 'unknown', i.e. as well for s(1) as for s(2). How can I do it (this is just a toy example, I do have a 123 dimensional struct where I do not want to do it one by one). Thanks

채택된 답변

Friedrich
Friedrich 2014년 4월 3일
Hi,
without a loop:
[s.name] = deal('unknown')
  댓글 수: 2
MiauMiau
MiauMiau 2014년 4월 3일
wow thanks
deejt
deejt 2021년 1월 19일
How would you proceed if you would only change the name 'anna' to unknown but leave 'bernd'?

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

추가 답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2014년 4월 3일
s(2) = struct('name','anna','age',18)
s(1) = struct('name','bernd','age',22)
n=numel(s)
for k=1:n
s(k).name='unknown'
end

카테고리

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