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

1 view (last 30 days)
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%%

Answers (1)

Florian Bidaud
Florian Bidaud on 28 Oct 2022
Edited: Florian Bidaud on 28 Oct 2022
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

Categories

Find more on Search Path in Help Center and File Exchange

Tags

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!