Why a field is replaced by next field when corresponding function is being called within a loop?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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%%
0 Kommentare
Antworten (1)
Florian Bidaud
am 28 Okt. 2022
Bearbeitet: Florian Bidaud
am 28 Okt. 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
0 Kommentare
Siehe auch
Kategorien
Mehr zu Structures finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!