Problem with Cell Arrays in GUIDE

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]

 Respuesta aceptada

Matt Fig
Matt Fig el 30 de Jun. de 2011
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 comentarios

B_Richardson
B_Richardson el 30 de Jun. de 2011
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 el 30 de Jun. de 2011
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.

Iniciar sesión para comentar.

Más respuestas (2)

Fangjun Jiang
Fangjun Jiang el 30 de Jun. de 2011
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 comentario

B_Richardson
B_Richardson el 30 de Jun. de 2011
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.

Iniciar sesión para comentar.

Fangjun Jiang
Fangjun Jiang el 30 de Jun. de 2011
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 comentarios

B_Richardson
B_Richardson el 30 de Jun. de 2011
Thank you. I will elaborate:
I eventually want to store the 1 x 50 cell array into an excel sheet.
Just to reiterate:
The popupmenu options, (1-10) form a column. And the listbox options form the rows (1-N) with multiple selections.
1. so user will chose a popupmenu options (1)
2. user will chose several of N corresponding listbox options
3. results saved to cell array
1. user will chose a popupmenu options (2)
2. user will chose several of N corresponding listbox options
3. results saved to cell array
and so on
I modified my code:
R = get(handles.popupmenu1,'val');
C = get(handles.listbox1,'val');
A = zeros(length(get(handles.popupmenu1,'string')),length(get(handles.listbox1,'string')));
%I took out the line below. Guess I didnt need it
%I = sub2ind(size(A),[R repmat(R,1,length(C))],[1 C]);
%took out this line. It was creating a new cell array each time instead of appending to the existing array
%Z = cell(1,50);
A(R,C) = 1
%took out both these lines as well
%A(I) = 1
%Z(:) = {I}
I dont think I need that for loop at all to accomplish what I need. What do you think?
B_Richardson
B_Richardson el 30 de Jun. de 2011
So if user selects option 10 from popupmenu and 2,6,10 from listbox, A looks like this:
A =
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 1 0 0 0 1 0 0 0 1 0 0
So I need to save each of these matrices in my 50 cells each time in order.
B_Richardson
B_Richardson el 30 de Jun. de 2011
somebody asked me this:
> Why are you using a 1-by-50 cell array, one cell of which contains an M-by-N
> matrix?
It's kind of a long story but I will explain. I have an excel sheet with roughly 50 lines of data. For each of those 50 lines, there is a corresponding video of a 10 rep exercise (popupmenu options represents the reps of each) that the user will watch.
The listbox contains labels to describe the correctness of the exercise (too fast, too slow, bending too much, leaning, ect). The labels are different for each of the 3 types of exercise.
So user will select video:
Listbox is populated with corresponding labels
User watches the video of the 10 reps
user selects a rep from the popupmenu
user selects multiple labels from listbox to describe that rep
selections are saved into matrix
matrix is saved into cells 1-50(in order)
cell array is eventually saved to cvs file
Important note* user will likely not label all videos at once, so I need to be able to pick up where left off and also labeling might not occur in order.
B_Richardson
B_Richardson el 30 de Jun. de 2011
@Matt Fig
Yes, I was mistaken yesterday I believe! My apologies! This is the correct array output:
if user selects option 10 from popupmenu and 2,6,10 from listbox, A looks like this:
A =
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 1 0 0 0 1 0 0 0 1 0 0
Matt Fig
Matt Fig el 30 de Jun. de 2011
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 el 30 de Jun. de 2011
Yes, that is correct! sorry for not being clearer at first!

Iniciar sesión para comentar.

Categorías

Etiquetas

Preguntada:

el 30 de Jun. de 2011

Community Treasure Hunt

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

Start Hunting!

Translated by