Hello everyone,
I have this code that writes zeros to a matrix based on the length of a listbox and popupmenu options. 1s are written to the matrix based on selections from the popupmenu and listbox:
function [] = pop_ex()
% Help goes here.
S.fh = figure('units','pixels',...
'position',[200 400 320 340],...
'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'});
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'))); %create matrix A, fill with zeros based on length of popup and list items
A(get(S.pp,'val'),get(S.ls,'val')) = 1;
%write 1s for selected items
A
%output A
The code works however, I want to append the matrix instead of creating a new one each time.

1 comentario

B_Richardson
B_Richardson el 12 de Jul. de 2011
%This line creates the matrix and fills it with zeros
A = zeros(length(get(S.pp,'string')),length(get(S.ls,'string')));
%this line writes 1s to the matrix based on the currently selected items from the listbox and popupmenu
A(get(S.pp,'val'),get(S.ls,'val')) = 1;
So the idea is upon multiple calls to this popupmenu callback (eventually this will be in a button separate from the popupmenu)
append A instead of creating a new A everytime

Iniciar sesión para comentar.

 Respuesta aceptada

Fangjun Jiang
Fangjun Jiang el 12 de Jul. de 2011

1 voto

So it's not really append A, rather it is setting one element of A to 1 one at a time. What you need is to load A from guidata at the beginning of the callback function. save A to guidata at the end of the callback function. The setting of A to all zeros is better to put in the CreateFcn callback.

4 comentarios

B_Richardson
B_Richardson el 12 de Jul. de 2011
%1st callback
A =
1 1 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
%2nd
A =
0 0 0 0 0 0
0 1 1 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
%3rd
A =
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 1 1 0
0 0 0 0 0 0
%4th
A =
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 1 1
%But this is actually what I want to accomplish
A =
1 1 0 0 0 0
0 1 1 0 0 0
0 0 1 1 0 0
0 0 0 0 1 1
B_Richardson
B_Richardson el 12 de Jul. de 2011
"So it's not really append A, rather it is setting one element of A to 1 one at a time."
%No, actually it is appending A. See above example
"What you need is to load A from guidata at the beginning of the callback function."
%Not sure if that is gonna work seeing as througout my program A changes length based on the listbox options
"save A to guidata at the end of the callback function."
%Yes, I agree
"The setting of A to all zeros is better to put in the CreateFcn callback."
%Why is that?
Fangjun Jiang
Fangjun Jiang el 13 de Jul. de 2011
That is what I meant. In your example, A doesn't change size. In every call back, two elements are changed. So in your callback function, you don't want to re-set it to be all zeros. The initialization of A is better to be put in the CreatFcn callback. Then, in this callback, every time you need to load the previous value of A first, and then change some of its elements and then save it back. That is the right programming flow, right?
B_Richardson
B_Richardson el 13 de Jul. de 2011
I understand now. Thank you!

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by