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 Center および File ExchangeMatrices and Arrays についてさらに検索

タグ

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by