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