Updating a Cell Array wit listbox and popupmenu options
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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!
채택된 답변
Matt Fig
2011년 6월 29일
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
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.
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Data Type Identification에 대해 자세히 알아보기
참고 항목
2011년 6월 29일
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
