matlab

[ matlab ] 구조체형 변수에서 필드 변수 추출

humming_stereo 2024. 11. 29. 14:39

서론

https://hummingstereo.tistory.com/38

 

구조체형 배열 자동 생성

이전에 나는 구조체형 배열 (struct)을 생성시 eval 함수를 이용하여 생성하였다. 과거 방식과 새로운 방식을 기술한다. num = 1:2:19; % old 1 for ii = 1:10 eval(['old.list_',num2str(ii,'%02d'),' = num(',num2str(ii),');'])

hummingstereo.tistory.com

이전에 구조체형 변수 배열을 반복문으로 만드는 방법을 작성했다.

그렇다면 구조체형 배열을 반대로 꺼낼 수 있는 방법 또한 찾아봤다.

 

 

방법

  • assignin: 지정된 공간에 변수값 할당

 

적용
clc; clear; close all;
% set value
data.name = 'John Doe';
data.age = 30;
data.height = 180;
data.weight = 75;

% get structure value's field value
id = fieldnames(data);
for i = 1:length(id)
% rename
    assignin('base', id{i}, data.(id{i}));
end

참고

https://kr.mathworks.com/help/matlab/ref/assignin.html

 

assignin

You clicked a link that corresponds to this MATLAB command: Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

kr.mathworks.com

 

 

'matlab' 카테고리의 다른 글

[matlab] 행렬 변환 (n*3 → a*b)  (0) 2025.01.13
[ matlab ] datetime 연산  (0) 2024.11.29
[ matlab ] 정해진 시간 간격으로 데이터 보정  (0) 2024.02.01
[ matlab ] yyaxis  (0) 2024.01.29
[ matlab ] cellfun  (0) 2024.01.27