Updating a Cell Array wit listbox and popupmenu options

Simple question: Given a cell array of: CellArray(1,50)={zeros(10,N)}
1. The 10 refers to 10 popupmenu options 2. The N refers to listboxoptions
So it's popupmenu options x listbox options
How can I input 1's for the currently selected listbox and popupmenu options? For instance:
popupmenu option #3 is selected; listbox options 4, 5, 7 are selected;
I want to produce this in my matrix based on those selections:
0 0 0 0 0 0 0 0 0 ..... N 0.......N 1 0 0 0 1 1 0 1 0 0 0 ....N 0.......N . . . . Please let me know if I'm not explaining this clearly!

 Accepted Answer

Is it that you have 10 popupmenus and N listboxes, or one popumenu and one listbox?
If you only have one of each, then shouldn't your array only have two ones in it?
Perhaps if you give a very small example...
%
%
%
EDIT In response to clarifying comments.
Here is an example, based on what you said. Note that the array is only displayed when a popup value is chosen, so select from the listbox first to fill in a column other than the first. You could alter this by putting the same code in the listbox callback...
function [] = pop_ex()
% Help goes here.
S.fh = figure('units','pixels',...
'position',[10 30 120 140],...
'menubar','none',...
'name','slider_ex',...
'numbertitle','off',...
'resize','off');
S.pp = uicontrol('style','pop',...
'unit','pix',...
'position',[20 20 120 40],...
'string',{'one','two','three','four'},...
'callback',@pp_call);
S.ls = uicontrol('style','list',...
'unit','pix',...
'position',[20 80 120 40],...
'string',{'lone','ltwo','lthree','lfour','lfive','lsix'});
guidata(S.fh,S)
function [] = pp_call(varargin)
% Callback for the popup.
S = guidata(gcbf);
A = zeros(length(get(S.pp,'string')),length(get(S.ls,'string')));
R = get(S.pp,'val');
C = get(S.ls,'val');
I = sub2ind(size(A),[R R],[1 C]);
A(I) = 1 % Display in command window.
%
%
%
%
EDIT Address multi-selectable listboxes.
If the listbox is multi-selctable, then use this line instead:
I = sub2ind(size(A),[R repmat(R,1,length(C))],[1 C]);

14 Comments

My apologies for not being clear.
I have one popupmenu that will always have 10 options.
I have one listbox that will be filled with N options.
So the 10 popupmenu options form a column and the rows are listbox options.
O.k., so then what about my second question? Say you have a popup with 2 options and a listbox with 3. Also say that the second option in the popup is chosen and the 2nd option is selected in the listbox. Then what would you expect your array to look like. Post the array for this simple example...
0000
1010
I think? right? I may be confused. But this is how I would want it to look.
Can you explain this code to me? I guess I'm not advanced enough yet to comprehend it all.
correct me if I'm wrong:
%call back for the popupmenu
function [] = pp_call(varargin
%what does this mean/do?
S = guidata(gcbf)
%create cell array with the column being the length of the popupmenu
and the rows the length of the listbox
A = zeros(length(get(S.pp,'string')),length(get(S.ls,'string')));
%store currently selected value in R
R = get(S.pp,'val');
%store currently selected value in C. Critical question here: If you have a listbox where multiple items can be selected at once, what form does 'val' take? does it become an array of those values selected?
C = get(S.ls,'val');
%I'm lost here. what does sub2ind do? And why are you using it?
I = sub2ind(size(A),[R R],[1 C]);
sorry for all the questions and thanks for your help!
1. Yes, this is the callback for the popup - my typo, now fixed.
2. This retrieves the handles structure made in the beginning.
3. Except I just used an array. Use num2cell.
4. Yes, store it in R
5. I will update with an edit for multi-selection listboxes.
6. SUB2IND does just what its help says it does.
I'm so bad at matlab it's not even funny. Your code runs fine but when I try to implement it in my GUI it doesnt work. I guess its because I'm using guide so I must be making errors with the handles.
I get this error:
??? Undefined function or variable 'popupmenu1'.
Error in ==> one>popupmenu1_Callback at 140
A = zeros(length(get(popupmenu1,'string')),length(get(listbox1,'string')));
Error in ==> gui_mainfcn at 96
feval(varargin{:});
Error in ==> one at 42
gui_mainfcn(gui_State, varargin{:});
Error in ==> @(hObject,eventdata)one('popupmenu1_Callback',hObject,eventdata,guidata(hObject))
??? Error while evaluating uicontrol Callback
I just put your code in the callback for the popupmenu and changed the names:
R = get(handles.popupmenu1,'val');
C = get(handles.listbox1,'val');
A = zeros(length(get(popupmenu1,'string')),length(get(listbox1,'string')));
I = sub2ind(size(A),[R R],[1 C]);
A(I) = 1; % Display in command window.
Nevermind, i just had to add "handles" before each listbox and popupmenu. Only thing now is it isnt outputing to the command window anymore. But the error are gone
To output to the command window, simply remove the semi-colon on the end. This won't put the variable in the base workspace (command window), but will display it there.
And that did it!!! Thank you so much! You are great!
one last question,
My actual cell array is:
CellArray(1,50)={zeros(M,N)}
So to get those 50 cells can I just use:
A(1,50)= {zeros(length(get(handles.popupmenu1,'string')),length(get(handles.listbox1,'string')))};
?
To fill all the elements of a cell with the same array use, for example:
C = cell(1,4)
C(:) = {zeros(5,6)}
I guess not. It's giving me these errors now:
??? Conversion to cell from double is not possible.
Error in ==> one>popupmenu1_Callback at 142
A(I) = 1 % Display in command window.
Error in ==> gui_mainfcn at 96
feval(varargin{:});
Error in ==> one at 42
gui_mainfcn(gui_State, varargin{:});
Error in ==> @(hObject,eventdata)one('popupmenu1_Callback',hObject,eventdata,guidata(hObject))
??? Error while evaluating uicontrol Callback
??? Error using ==> sub2ind at 58
Out of range subscript.
Error in ==> one>popupmenu1_Callback at 141
I = sub2ind(size(A),[R repmat(R,1,length(C))],[1 C]);
Error in ==> gui_mainfcn at 96
feval(varargin{:});
Error in ==> one at 42
gui_mainfcn(gui_State, varargin{:});
Error in ==> @(hObject,eventdata)one('popupmenu1_Callback',hObject,eventdata,guidata(hObject))
I'm not sure if I understand that. So are you saying to use the array "A" to fill my cell array, "C(1,50)" ?
So it would be:
C(:) = {A}?
So I came up with this:
R = get(handles.popupmenu1,'val');
C = get(handles.listbox1,'val');
A = zeros(length(get(handles.popupmenu1,'string')),length(get(handles.listbox1,'string')));
I = sub2ind(size(A),[R repmat(R,1,length(C))],[1 C]);
Z = cell(1,50);
A(I) = 1
for i = 1: 50
Z(:) = {A}
end
The only problem is that it saves all of the listbox options and all of the popupmenu options instead of just the currently selected items.
But it does store the array A into the 1 x 50 cell array Z so I guess its a start.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!