sum filed in struct

4 views (last 30 days)
Luca Re
Luca Re on 3 Jun 2023
Edited: Paul on 6 Jun 2023
>>Sis.PV
ans =
20
ans =
20
ans =
20
Sis =
1×82 struct array with fields:
Name
On
PV
>> sum(Sis.PV)
Error using sum
Too many input arguments.

Accepted Answer

Paul
Paul on 3 Jun 2023
Edited: Paul on 6 Jun 2023
% example data
Sis.PV = 20;
% Sis.PV = repmat(Sis.PV,1,82); % original incorrect line, expanded the field instead of the struct
Sis = repmat(Sis,1,82); % correct line, expands the struct
Sis
Sis = 1×82 struct array with fields:
PV
sum([Sis.PV])
ans = 1640
See this tutorial on how to use comma separated lists.
  3 Comments
Paul
Paul on 3 Jun 2023
Yes, I used []. Read the tutorial at the link I provided in the answer for more info. And/or search the doc for "comma-separated lists".
Paul
Paul on 6 Jun 2023
I just realized I had a mistake in my example. I'll edit the Answer.

Sign in to comment.

More Answers (0)

Categories

Find more on Structures in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!