Sum of values stored in an array of handles

3 views (last 30 days)
I got the following callback function
function solveclick(bl) %bl is an array of handles to pushbuttons (created with uicontrol)
for i=2:10
rows(i)=sum(get(bl(i,:),'value'));
end
end
What I'm trying to do is add the values of the pushbuttons in a row and store the individual sums in a vector called rows.

Accepted Answer

Image Analyst
Image Analyst on 1 Apr 2020
Are you SURE you set the Value property of the buttons when you created them? You'd need to have done that for your code, though I think it's unusual. Most people would set the UserData property, or just keep a separate array for whatever numbers you want to store. Are you using GUIDE or App Designer?
  2 Comments
Juri Augsburger
Juri Augsburger on 2 Apr 2020
>> get(bimarugui(2,1),'userdata')
ans =
0
>> whos ans
Name Size Bytes Class Attributes
ans 1x1 8 double
bimarugui is the handle outside the function.
when i run the function and look what's in userdata I get the right answer (a double variable)
I believe there's a problem with the "(1,:)" notation, but how to write this differently?
The error message displayed:
Error using sum
Invalid data type. First argument must be numeric or logical.
Error in BimaruGUI>solveclick (line 156)
rows(i)=sum(get(bl(i,:),'userdata'));
Error while evaluating UIControl Callback.
Juri Augsburger
Juri Augsburger on 2 Apr 2020
I got it!! cell2mat before get(...) !

Sign in to comment.

More Answers (1)

Geoff Hayes
Geoff Hayes on 1 Apr 2020
Juri - the only problem that might exist for you (it did for me when i tried this) was that
get(bl(i,:),'value')
returned a cell array of numeric values which, when trying to sum that array, I observed a Undefined function 'sum' for input arguments of type 'cell' error message. To get around this, you would need to convert to a matrix
rows(i)=sum(cell2mat(get(bl(i,:),'value')));
  1 Comment
Juri Augsburger
Juri Augsburger on 2 Apr 2020
I tried that already, but it didnt work. I'll try the userdata property as suggested in the comment below. thank you!

Sign in to comment.

Categories

Find more on Migrate GUIDE Apps 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!