Sum of values stored in an array of handles

3 次查看(过去 30 天)
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.

采纳的回答

Image Analyst
Image Analyst 2020-4-1
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 个评论
Juri Augsburger
Juri Augsburger 2020-4-2
>> 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.

请先登录,再进行评论。

更多回答(1 个)

Geoff Hayes
Geoff Hayes 2020-4-1
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 个评论
Juri Augsburger
Juri Augsburger 2020-4-2
I tried that already, but it didnt work. I'll try the userdata property as suggested in the comment below. thank you!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by