Dear all,
I have a function that needs to treat data coming from a structure, and I want to have several inputs indicating which branch of the structure is the one to be treated:
function total = totalCampus(campus,building,year,var)
total=getfield(campus,(year),(building),(var))
end
This is just the first line of the function, where I want to call the specific column of the table in the structure, using dynamic fieldnames, but I can't manage. Any clue?
best regards,
Mario

 採用された回答

Adam
Adam 2019 年 5 月 2 日

0 投票

total = campus.( building ).( year ).( var );
should work fine, assuming campus is your struct and that is a branch of the struct.

3 件のコメント

Mario Prinetti
Mario Prinetti 2019 年 5 月 6 日
function total = totalCampus(campus,year,type,var)
switch campus
case 1
building={'sabatini','quevedo','betancourt','biblioAud','benet'};
total=zeros(34492,length(building));
for n=1:length(building)
total(:,n)=leganes.(year).building{n}.(type).(var);
end
total=nansum(total(total>0),'all');
This is my actual code, used to add up all values of the selected variable (it ahhas a second case for another campus). The issue is it doesn't seem to read the year correctly (the name of the structure branch is a2017 for example). once I type tot=totalCampus(1,a2017,total,PotenciaActivaTotal_kW); it tells me that the function or variable a2017 is undefined
Adam
Adam 2019 年 5 月 7 日
Is it defined?
Stephen23
Stephen23 2019 年 5 月 7 日
編集済み: Stephen23 2019 年 5 月 7 日
"once I type tot=totalCampus(1,a2017,total,PotenciaActivaTotal_kW); it tells me that the function or variable a2017 is undefined "
You are typing those inputs without single quotes, which tells MATLAB that they are variable/function/class/etc. names. You need to use single quotes to define character vectors.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeDeep Learning Toolbox についてさらに検索

製品

リリース

R2018b

質問済み:

2019 年 5 月 2 日

編集済み:

2019 年 5 月 7 日

Community Treasure Hunt

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

Start Hunting!

Translated by