I'm trying to create a shopping list using buttons and am having trouble getting my list to stack items

5 views (last 30 days)
What I want the program to do:
Click the meal that you want to add to the list --> The button callback f'n adds to the global list --> Click done and the total list of ingredients prints to the command window
What I have tried is making a matrix that will be added to by each button TOTAL=zeros(25,1). (For 25 ingredients). When I hit the button it does add to TOTAL, but if I hit another button it adds to the original TOTAL which is just all zeros.
Here is the global matrix:
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
ING=['Onion';'Garlic';'Eggs'; 'Bell Pepper';'Oatmeal';'Pasta';'Pasta Sause';'Mushrooms';'Cornmeal';'Self Rise Flour';'Canned Milk';'Sour Cream'; 'Milk';'Cream Corn';'Bagel';'Cereal';'Kidney Beans';'Black Beans';'Ground Beef';'Tomato Paste';'Cornbeef';'Cabage';'Carrots'];
TOTAL=[0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0]; %the zeros coorespond to the quantity.
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
This is my button callback:
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
function oatmeal_fn(Oatmeal_h, evt, TOTAL)
ing=['Oatmeal'];
quant=[0;0;0;0;1;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0;0];
TOTAL+quant
endfunction
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
It adds the matrices correctly but when I click another button it adds to the original total.
  5 Comments
Walter Roberson
Walter Roberson on 28 Feb 2019
You should be asking your question on the Octave mailing list, as we do not have the resources to deal with Octave here.
(endfunction is never used in MATLAB.)
Andrew Lorick
Andrew Lorick on 2 Mar 2019
Shoot! My cover's blown!! Haha, it didn't even occur to me to ask on the Octave forums until after I posted the question. I've learned lots though, so thank you!

Sign in to comment.

Answers (1)

Kevin Phung
Kevin Phung on 15 Feb 2019
Edited: Kevin Phung on 15 Feb 2019
some suggestions:
function oatmeal_fn(Oatmeal_h, evt, TOTAL) % i would remove TOTAL if you could,
% and instead replace it with the figure handle if it is possible,
%else, retrieve figure handle with 'gcf' (gets current figure)
f = gcf; % your figure handle
total = f.UserData % you should set the user data to be your TOTAL matrix outside of this function;
% this line retrieves this information
ind = 5; %this is the index for oatmeal;
total(ind) = total(ind)+ 1 % adds one to oatmeal
f.UserData = total; %saves the matrix into User data, to be used by other buttons.
end
this is assuming you have 24 other callbacks.
  4 Comments
Andrew Lorick
Andrew Lorick on 28 Feb 2019
So, outside of the callback fn I set:
TOTAL=get(f,'userdata);
function oatmeal_fn(Oatmeal_h, evt, f)
total=get(f,'userdata');
ind=5;
total(ind)=total(ind)+1; %So here we are saying total(5) which is total(row 5)?
set(f,'userdata',total);
endfunction
When I run this, I get an error: "binary operator '+' not implemented for 'cell' by 'scalar' operations"
I understand that 'userdata' is a cell array and not a matrix, so I tried using cell2mat:
function oatmeal_fn(Oatmeal_h, evt, f)
total=get(f,'userdata');
cell2mat(total);
ind=5;
total(ind)=total(ind)+1;
set(f,'userdata',total);
endfunction
Unfortunately, I get the same error.
Kevin Phung
Kevin Phung on 28 Feb 2019
no, whichever line you created your figure along with its handle, you should be setting
f.UserData = zeros(1,numel(ING)) %this will represent your total. where f is your figure handle
the variable 'total' is only within the scope of each callback.
function oatmeal_fn(Oatmeal_h, evt, f)
total=get(f,'userdata');
ind=5;
total(ind)=total(ind)+1; %So here we are saying total(5) which is total(row 5)?
set(f,'userdata',total);
end
if for the sake of less lines of code but less clarity:
function oatmeal_fn(Oatmeal_h, evt, f)
ind=5;
f.UserData(ind) = T.UserData(ind) + 1;
end

Sign in to comment.

Categories

Find more on Interactive Control and Callbacks 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!