How could I possibly iterate over three 3D arrays and use their variable names iteratively in the title and axes?

조회 수: 2 (최근 30일)
x = freq;
idx_phi = find((phi_vec) == 0);
idx_theta = find((theta_vec) == 0);
v_dx = {EField_h(idx_phi,idx_theta,:), EField_v(idx_phi,idx_theta,:),EField_a(idx_phi,idx_theta,:)}
for i = 1: numel(v_dx)
y = squeeze(abs(v_dx{i}));
plot(x,((y).^2)/50,'-')
title(num2str(v_dx{i})) % Here I want to iterate over the variable names: EField_h, EField_v, EField_a
xlim([fc-0.5 fc+0.5]);
xlabel('frequency');
ylabel(v_dx{i},''); % Here I want to iterate over the variable names: EField_h, EField_v, EField_a
end
  댓글 수: 3
AY
AY 2022년 9월 16일
편집: AY 2022년 9월 16일
I am able to loop over the field names now but not over the contents inside those fieldnames ....fieldnames contain cell arrays how can i access the contents inside the cell array and loop them as well without defining extra variables
v_dx = struct('EField_h', [EField_h(idx_phi,idx_theta,:)], 'EField_v', [EField_h(idx_phi,idx_theta,:)], 'EField_a', [EField_h(idx_phi,idx_theta,:)])
for k = 1: numel(Fields)
y = squeeze(abs(Fields{k})); Here I want to access the values of fields
figure;
plot(x,((y).^2)/50,'-')
title(Fields{k})
xlim([fc-0.5 fc+0.5]);
xlabel('frequency');
ylabel(Fields{k});
end

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

채택된 답변

Jan
Jan 2022년 9월 16일
편집: Jan 2022년 9월 16일
v_dx = struct('EField_h', EField_h(idx_phi,idx_theta,:), ...
'EField_v', EField_h(idx_phi,idx_theta,:), ...
'EField_a', EField_h(idx_phi,idx_theta,:));
Fields = fieldnames(v_dx);
for k = 1:numel(Fields)
value = v_dx.(Fields{k});
y = squeeze(abs(value));
...
This is called "dynamic fieldnames". The parentheses around the field name are important.
By the way, there is no need to include an array in square brackets. [] is Matlab's operator for concatenating arrays. [x] concatenates x with nothing, so simply write x.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by