Why a field is replaced by next field when corresponding function is being called within a loop?

조회 수: 1 (최근 30일)
parameter = string(["Length","Width"]);
for m = 1 : length(parameter)
[SL_CI,DL_CI] = Condition_Index (file_list_damage,damageinfo_selected_yr,increment_vector,increment(m),(parameter{m));%
plot(x,SL_CI);
end
function [SL_CI,DL_CI] = Condition_Index (file_list_damage,damageinfo_selected_yr,increment_vector,increment,parameter)
%%some other calculation steps which gives the input for following function call%%
SL_CI.(parameter{m}) = UK_CI_SL(file_list_damage,damageinfo_selected_yr,DL_CI,ImpFactor); %this calls out a function which returns a double array%%
end
% now this code gives me a struct SL_CI with only one field 'Width',
% replacing the field 'Length'%%
%% I want both the field stored, if you guys have any idea on this, I would greatly appreciate it%%

답변 (1개)

Florian Bidaud
Florian Bidaud 2022년 10월 28일
편집: Florian Bidaud 2022년 10월 28일
Hi,
You're using the same [SL_CI,DL_CI] to store both fields, so the last one override the first one.
You should have storing variables incrementing with m like :
for m = 1 : length(parameter)
[SL_CI(m),DL_CI(m)] = Condition_Index (file_list_damage,damageinfo_selected_yr,increment_vector,increment(m),(parameter{m}));%
plot(x,SL_CI);
end
I don't know what if the output type, so you might have to use a cell array, but this is the idea.
You shouldn't have m in the function deifinition
function [SL_CI,DL_CI] = Condition_Index (file_list_damage,damageinfo_selected_yr,increment_vector,increment,parameter)
%%some other calculation steps which gives the input for following function call%%
SL_CI.(parameter) = UK_CI_SL(file_list_damage,damageinfo_selected_yr,DL_CI,ImpFactor); %this calls out a function which returns a double array%%
end

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

태그

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by