Problem with Cell Arrays in GUIDE

2 次查看(过去 30 天)
I am attempting to fill a cell array as follows: CellArray(1,50)={zeros(M,N)} With M being popupmenu options and N being listbox options.
so far I have this code which does work to a certain extent. It takes listbox options and popupmenu options and stores them into array. popupmenu options are columns and listbox options are rows. I am attempting to use a for loop to store the currently selected items into my cell array, Z:
R = get(handles.popupmenu1,'val');
%listbox accepts multiple selections*
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);
for i = 1: 50
Z(:) = {I}
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. So the question is how would i need to modify this code in order to into my 1 by 50 cell array?
Here is what the output looks like:
Z =
Columns 1 through 7
[1x7 double] [1x7 double] [1x7 double] [1x7 double] [1x7 double] [1x7 double] [1x7 double]
Columns 8 through 14
[1x7 double] [1x7 double] [1x7 double] [1x7 double] [1x7 double] [1x7 double] [1x7 double]
Columns 15 through 21
[1x7 double] [1x7 double] [1x7 double] [1x7 double] [1x7 double] [1x7 double] [1x7 double]
Columns 22 through 28
[1x7 double] [1x7 double] [1x7 double] [1x7 double] [1x7 double] [1x7 double] [1x7 double]
Columns 29 through 35
[1x7 double] [1x7 double] [1x7 double] [1x7 double] [1x7 double] [1x7 double] [1x7 double]
Columns 36 through 42
[1x7 double] [1x7 double] [1x7 double] [1x7 double] [1x7 double] [1x7 double] [1x7 double]
Columns 43 through 49
[1x7 double] [1x7 double] [1x7 double] [1x7 double] [1x7 double] [1x7 double] [1x7 double]
Column 50
[1x7 double]

采纳的回答

Matt Fig
Matt Fig 2011-6-30
O.k., So now I will make another simple example. You tell me if we are finally on the same page ;-)! This example fills a 1-by-3 cell instead of a 1-by-50. You can modify it for your needs.
function [] = pop_ex()
% Help goes here.
S.fh = figure('units','pixels',...
'position',[100 300 120 140],...
'menubar','none',...
'name','slider_ex',...
'numbertitle','off',...
'resize','off');
S.pp = uicontrol('style','pop',...
'unit','pix',...
'position',[20 20 80 40],...
'string',{'one','two','three','four'},...
'callback',@pp_call);
S.ls = uicontrol('style','list',...
'unit','pix',...
'position',[20 80 80 40],...
'min',0,'max',5,...
'string',{'lone','ltwo','lthree','lfour','lfive','lsix'});
S.C = cell(1,3);
S.cnt = 1;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')));
A(get(S.pp,'val'),get(S.ls,'val')) = 1;
S.C(S.cnt) = {A}; % Update this particular element of the cell.
S.cnt = S.cnt + 1; % Increment the counter.
guidata(S.fh,S) % Resave the structure so updates are not lost.
S.C{:} % Show in the command window.
  2 个评论
B_Richardson
B_Richardson 2011-6-30
I do believe this is correct! Can I ask a few questions to make sure I understand everything and have learned from your code?
%1. create cell array
S.C = cell(1,3);
% 2. what does this line do?
S.cnt = 1;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')));
%3. what is this doing?
A(get(S.pp,'val'),get(S.ls,'val')) = 1;
S.C(S.cnt) = {A}; % Update this particular element of the cell.
S.cnt = S.cnt + 1; % Increment the counter.
guidata(S.fh,S) % Resave the structure so updates are not lost.
S.C{:} % Show in the command window
Matt Fig
Matt Fig 2011-6-30
Line 2 saves the count, so that each time the user chooses from the popup the count is incremented. That GUIDATA bit should be on a separate line, but the pasting got mixed up. This saves the structure so it can be accessed later in the callback.
Line 3 is assigning the ones to the array. Since you clarified what you want, we don't need to use SUB2IND.
Now if you need to save the cell array once it is full, you should put a line in there that checks if the count is equal to 50, and if so save the cell array.

请先登录,再进行评论。

更多回答(2 个)

Fangjun Jiang
Fangjun Jiang 2011-6-30
I must say I didn't try to understand all of your discussion. From all of your three questions, may I suggest a different approach? If you put a uitable in the GUI and let the user fill the data, would it be easier? Every time the user fill the data and then press a button, you can save the data. I understand that the user may need to do this 50 times, which is not easy. But in your current way, the user also need to do 50 times, right? It's probably not a good UI. I can't imaging any user doing it 50 times.
Just a thought! Below is a sample code.
h=uitable('Data',zeros(10),'ColumnEditable',true(1,10),'ColumnWidth',repmat({25},1,10))
Fill a few cells and then run
Data=get(h,'Data')
  1 个评论
B_Richardson
B_Richardson 2011-6-30
Yes, originally I was going to use a table. However, I dont have space on the window. I have to display the video, an axes with data from sensors that the people wear in the video, and the movie play controls. The uitable would not have fit.

请先登录,再进行评论。


Fangjun Jiang
Fangjun Jiang 2011-6-30
Should the line inside the for-loop be this ?
Z(i)={I};
And questions: what is the typical value of get(handles.popupmenu1,'string') and get(handles.listbox1,'string')?
  6 个评论
Matt Fig
Matt Fig 2011-6-30
O.k., so now it looks like there are multiple confusions. You do or do not want all 50 elements of the cell to be filled with the same A? As I understand it now, you want to have a different A saved in the cell each time the user selects from the popup, for a total of 50 different saves. Is that correct?
B_Richardson
B_Richardson 2011-6-30
Yes, that is correct! sorry for not being clearer at first!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Interactive Control and Callbacks 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by