Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Load Listbox from Array

Subject: Load Listbox from Array

From: Maxx Chatsko

Date: 30 Jul, 2010 14:24:04

Message: 1 of 21

Hello all,
    I created a gui with guide. A listbox in the main figure was set by typing the list in the STRING edit in Property Inspector, but the list may be upgraded from time to time. So I'm attempting to load the listbox from an array created with xlsread. In [num, txt]=xlsread() the txt is a cell array, but it doesn't load into the listbox as a cell array. Or a struct, which is what I think it wants. My code in the CreateFcn for the listbox is:

set(handles.listbox1,'string',substance_listbox1);

is this wrong? How do I set the listbox?
    Thanks
    Maxx

Subject: Load Listbox from Array

From: Maxx Chatsko

Date: 30 Jul, 2010 15:02:05

Message: 2 of 21

When I run the program I get the following error.

    Attempt to reference field of non-structure array.
Error in ==> PSLprcsr>listbox1_CreateFcn at 61
set(handles.listbox1, 'string', substance_listbox1);

It appears when the array I pass to set or uicontrol is a cell or char...

Subject: Load Listbox from Array

From: Walter Roberson

Date: 30 Jul, 2010 15:18:04

Message: 3 of 21

Maxx Chatsko wrote:
> When I run the program I get the following error.
>
> Attempt to reference field of non-structure array.
> Error in ==> PSLprcsr>listbox1_CreateFcn at 61
> set(handles.listbox1, 'string', substance_listbox1);
>
> It appears when the array I pass to set or uicontrol is a cell or char...

No, the implication is that "handles" is not a structure.

Subject: Load Listbox from Array

From: Maxx Chatsko

Date: 30 Jul, 2010 15:30:25

Message: 4 of 21

Walter Roberson <roberson@hushmail.com>

> No, the implication is that "handles" is not a structure.

Right I was saying that it appears for both cases not that it is referring to the cell or char. But that doesn't really help me solve my dilemna....I use set in other places in my code with handles and it works fine. So how do I set a listbox?
      Thank you for your input.

Subject: Load Listbox from Array

From: Walter Roberson

Date: 30 Jul, 2010 16:25:42

Message: 5 of 21

Maxx Chatsko wrote:
> Walter Roberson <roberson@hushmail.com>
>
>> No, the implication is that "handles" is not a structure.
>
> Right I was saying that it appears for both cases not that it is
> referring to the cell or char. But that doesn't really help me solve my
> dilemna....I use set in other places in my code with handles and it
> works fine. So how do I set a listbox?

We need to see the set() in context.

Subject: Load Listbox from Array

From: Maxx Chatsko

Date: 30 Jul, 2010 16:38:04

Message: 6 of 21

Walter Roberson <roberson@hushmail.com>
> We need to see the set() in context.

function listbox1_CreateFcn(hObject, eventdata, handles)
    [substance,substance]=xlsread('PSL Master Sheet.xlsx','SWIR',...
                            'A2:A10000');

set(handles.listbox1,'UserData',substance);

     I noticed that cells were under UserData in the Property Inspector so I tried changing 'string' to 'userdata', but no dice. It doesn't work with string either. Also tried both with cell and char. Same result...

Subject: Load Listbox from Array

From: ImageAnalyst

Date: 30 Jul, 2010 16:49:11

Message: 7 of 21

On Jul 30, 11:30 am, "Maxx Chatsko" <chats...@chemimage.com> wrote:
> Walter Roberson <rober...@hushmail.com>
>
> > No, the implication is that "handles" is not a structure.
>
> Right I was saying that it appears for both cases not that it is referring to the cell or char. But that doesn't really help me solve my dilemna....I use set in other places in my code with handles and it works fine. So how do I set a listbox?
>       Thank you for your input.
----------------------------------------------------------------------------------------------------------------------------------------------

I believe a listbox can only take an N rows by 1 column cell array.
Do you get this shape of cell array when you read in your Excel file?
It will probably fail if your data is not in one single column in your
spreadsheet.

Subject: Load Listbox from Array

From: Maxx Chatsko

Date: 30 Jul, 2010 17:25:06

Message: 8 of 21

ImageAnalyst <imageanalyst@mailinator.com> >

> I believe a listbox can only take an N rows by 1 column cell array.
> Do you get this shape of cell array when you read in your Excel file?
> It will probably fail if your data is not in one single column in your
> spreadsheet.

   Hmmm. My array is just a list of pharmaceutical agents with one column. In Property Inspector you can set the number of columns and rows under UserData cell. I tried changing it off the default (1x0) but it didn't work. This doesn't seem too difficult, but of course its proving to be. Are there settings in Property Inspector or guide that I might be overlooking?

Subject: Load Listbox from Array

From: Maxx Chatsko

Date: 30 Jul, 2010 18:08:04

Message: 9 of 21

> ImageAnalyst <imageanalyst@mailinator.com> >

   I think the problem is that all of the names are different lengths as a cell array. So I converted it to a structure and read them into the listbox with a for loop. Debugging in progress. Anyone wanna test my theory with me feel free...

Subject: Load Listbox from Array

From: Walter Roberson

Date: 31 Jul, 2010 05:35:06

Message: 10 of 21

Maxx Chatsko wrote:
> Walter Roberson <roberson@hushmail.com>
>> We need to see the set() in context.
>
> function listbox1_CreateFcn(hObject, eventdata, handles)
> [substance,substance]=xlsread('PSL Master Sheet.xlsx','SWIR',...
> 'A2:A10000');
>
> set(handles.listbox1,'UserData',substance);
>
> I noticed that cells were under UserData in the Property Inspector
> so I tried changing 'string' to 'userdata', but no dice. It doesn't work
> with string either. Also tried both with cell and char. Same result...

I suggest you put a breakpoint in at the set() line, and examine the
value of handles to see whether it is what is you expect.

Subject: Load Listbox from Array

From: GUIBuilder

Date: 21 Jun, 2011 20:08:19

Message: 11 of 21

"machatsk" wrote in message <i2una4$697$1@fred.mathworks.com>...
> Hello all,
> I created a gui with guide. A listbox in the main figure was set by typing the list in the STRING edit in Property Inspector, but the list may be upgraded from time to time. So I'm attempting to load the listbox from an array created with xlsread. In [num, txt]=xlsread() the txt is a cell array, but it doesn't load into the listbox as a cell array. Or a struct, which is what I think it wants. My code in the CreateFcn for the listbox is:
>
> set(handles.listbox1,'string',substance_listbox1);
>
> is this wrong? How do I set the listbox?
> Thanks
> Maxx

Maxx, did you ever solve this issue? I am trying to do the same thing now. If so, can you provide the code?

Thanks,

Subject: Load Listbox from Array

From: Maxx Chatsko

Date: 21 Jun, 2011 20:32:02

Message: 12 of 21

> Maxx, did you ever solve this issue? I am trying to do the same thing now. If so, can you provide the code?
>
> Thanks,

This is the code for the listbox1_CreateFcn, an update push button I put on the GUI, and the main Start push button I put on the GUI. Hope this helps. I know it can be confusing looking through other people's code, so let me know if you have any questions.
               Maxx

%--------------------------------------------------------------------------
% Pure component listbox

%--------------------------------------------------------------------------

function listbox1_CreateFcn(hObject, eventdata, handles)

[substance,substance]=xlsread('PSL Master Sheet.xlsx','Sample Information',...

'A3:A10000');

set(hObject,'string',substance);

end %function listbox1_CreateFcn()





function update_push_Callback(hpush, eventdata, handles) %Update button I put on GUI

[substance,substance]=xlsread('PSL Master Sheet.xlsx','Sample Information',...

'A3:A10000');

set(handles.listbox1,'string',substance);


msgbox('The listbox has been updated.','Update Complete','help');

end %function update_push_Callback()



%--------------------------------------------------------------------------

% Extract TIF paths from master spreadsheet

%--------------------------------------------------------------------------

function start_push_Callback(hObject, eventdata, handles)


%Close open figures

Lfig=length(findall(0,'type','figure'));

j=0;

for j=2:(Lfig)

close(j-1)

end




%Get selection from listbox

val_lbox=get(handles.listbox1,'value');

Subject: Load Listbox from Array

From: GUIBuilder

Date: 22 Jun, 2011 17:05:20

Message: 13 of 21

"Maxx Chatsko" wrote in message <itqv42$dai$1@newscl01ah.mathworks.com>...

Thanks this is a big help! I just have a few questions below:


>
> %--------------------------------------------------------------------------
> % Pure component listbox
>
> %--------------------------------------------------------------------------


> function listbox1_CreateFcn(hObject, eventdata, handles)
>
> [substance,substance]=xlsread('PSL Master Sheet.xlsx','Sample Information',...
>
> 'A3:A10000');

what is this "Sample Information',....'A:1000');" ?
And this [substance,substance]?

How does this work? It looks to me like you read from the xls sheet into [substance,substance] (is this an array?). Are you doing this because you have 2 columns of data?

> set(hObject,'string',substance);
>
> end %function listbox1_CreateFcn()
>
>
>
>
>
> function update_push_Callback(hpush, eventdata, handles) %Update button I put on GUI
>
> [substance,substance]=xlsread('PSL Master Sheet.xlsx','Sample Information',...
>
> 'A3:A10000');
>
> set(handles.listbox1,'string',substance);
>
>
> msgbox('The listbox has been updated.','Update Complete','help');
>
> end %function update_push_Callback()
>
>
>
> %--------------------------------------------------------------------------
>
> % Extract TIF paths from master spreadsheet
>
> %--------------------------------------------------------------------------
>
> function start_push_Callback(hObject, eventdata, handles)

Can you explain what this loop is doing?

> %Close open figures
>
> Lfig=length(findall(0,'type','figure'));
>
> j=0;
>
> for j=2:(Lfig)
>
> close(j-1)
>
> end
>
>
>
>
> %Get selection from listbox
>
> val_lbox=get(handles.listbox1,'value');

thank you, and sorry if these are stupid questions.

Subject: Load Listbox from Array

From: GUIBuilder

Date: 22 Jun, 2011 17:21:02

Message: 14 of 21

I guess I should tell you that my xls sheet is just 1 column of strings.

Subject: Load Listbox from Array

From: Maxx Chatsko

Date: 22 Jun, 2011 17:33:06

Message: 15 of 21

My example was specific to my code. See my respones below:

> > [substance,substance]=xlsread('PSL Master Sheet.xlsx','Sample Information',...
> > 'A3:A10000');
>
> what is this "Sample Information',....'A:1000');" ?
> And this [substance,substance]?
*snip
1) 'Sample Information' is the name of the sheet/tab in the Excel File.
2) A3:A100000 tells xlsread to get all the data in column A rows 3:100000 or basically all of column A.

> How does this work? It looks to me like you read from the xls sheet into [substance,substance] (is this an array?). Are you doing this because you have 2 columns of data?

*snip
3) [substance,substance] are the outputs of xlsread aka what is in the Excel sheet. I named both outputs as "substance" because I couldn't get it to work with just one variable I think. It didn't matter for my data because my Excel sheet contained pathnames and strings...

> > %Close open figures
> > Lfig=length(findall(0,'type','figure'));
> > j=0;
> > for j=2:(Lfig)
> > close(j-1)
> > end
>
> Can you explain what this loop is doing?
*snip
4) This loop simply finds all open figures (plots) generated from previous runs of the GUI and closes them upon running the GUI again. I did this so I didn't have 100 plots open from previous runs - it can be pretty annoying.

      Maxx

Subject: Load Listbox from Array

From: GUIBuilder

Date: 22 Jun, 2011 17:52:20

Message: 16 of 21

"Maxx Chatsko" wrote in message <itt90i$1k2$1@newscl01ah.mathworks.com>...
> My example was specific to my code. See my respones below:
>
> > > [substance,substance]=xlsread('PSL Master Sheet.xlsx','Sample Information',...
> > > 'A3:A10000');
> >
> > what is this "Sample Information',....'A:1000');" ?
> > And this [substance,substance]?
> *snip
> 1) 'Sample Information' is the name of the sheet/tab in the Excel File.
> 2) A3:A100000 tells xlsread to get all the data in column A rows 3:100000 or basically all of column A.

I see. I wasn't paying attention. You have a master sheet with tabs. I just have one sheet. So I wont need all of this.

> > How does this work? It looks to me like you read from the xls sheet into [substance,substance] (is this an array?). Are you doing this because you have 2 columns of data?
>
> *snip
> 3) [substance,substance] are the outputs of xlsread aka what is in the Excel sheet. I named both outputs as "substance" because I couldn't get it to work with just one variable I think. It didn't matter for my data because my Excel sheet contained pathnames and strings...
>
> > > %Close open figures
> > > Lfig=length(findall(0,'type','figure'));
> > > j=0;
> > > for j=2:(Lfig)
> > > close(j-1)
> > > end
> >
> > Can you explain what this loop is doing?
> *snip
> 4) This loop simply finds all open figures (plots) generated from previous runs of the GUI and closes them upon running the GUI again. I did this so I didn't have 100 plots open from previous runs - it can be pretty annoying.

I see. This is just for convenience.
> Maxx

Subject: Load Listbox from Array

From: matt dash

Date: 22 Jun, 2011 18:06:04

Message: 17 of 21

"GUIBuilder " <ericjaycorbett@gmail.com> wrote in message <itta4k$5b7$1@newscl01ah.mathworks.com>...
> "Maxx Chatsko" wrote in message <itt90i$1k2$1@newscl01ah.mathworks.com>...
> > My example was specific to my code. See my respones below:
> >
> > > > [substance,substance]=xlsread('PSL Master Sheet.xlsx','Sample Information',...
> > > > 'A3:A10000');
> > >
> > > what is this "Sample Information',....'A:1000');" ?
> > > And this [substance,substance]?
> > *snip
> > 1) 'Sample Information' is the name of the sheet/tab in the Excel File.
> > 2) A3:A100000 tells xlsread to get all the data in column A rows 3:100000 or basically all of column A.
>
> I see. I wasn't paying attention. You have a master sheet with tabs. I just have one sheet. So I wont need all of this.
>
> > > How does this work? It looks to me like you read from the xls sheet into [substance,substance] (is this an array?). Are you doing this because you have 2 columns of data?
> >
> > *snip
> > 3) [substance,substance] are the outputs of xlsread aka what is in the Excel sheet. I named both outputs as "substance" because I couldn't get it to work with just one variable I think. It didn't matter for my data because my Excel sheet contained pathnames and strings...
> >
> > > > %Close open figures
> > > > Lfig=length(findall(0,'type','figure'));
> > > > j=0;
> > > > for j=2:(Lfig)
> > > > close(j-1)
> > > > end
> > >
> > > Can you explain what this loop is doing?
> > *snip
> > 4) This loop simply finds all open figures (plots) generated from previous runs of the GUI and closes them upon running the GUI again. I did this so I didn't have 100 plots open from previous runs - it can be pretty annoying.
>
> I see. This is just for convenience.
> > Maxx

Isn't the answer to the original post simply that create functions are run before handles is fillled, so you can't refer to handles within your create function. (well you can, but it'll be empty). Is that the same problem you're having?

Subject: Load Listbox from Array

From: Maxx Chatsko

Date: 22 Jun, 2011 19:41:04

Message: 18 of 21

> Isn't the answer to the original post simply that create functions are run before handles is fillled, so you can't refer to handles within your create function. (well you can, but it'll be empty). Is that the same problem you're having?

Yes, you are correct. I just gave some code as a starting point.

        Maxx

Subject: Load Listbox from Array

From: GUIBuilder

Date: 22 Jun, 2011 19:41:04

Message: 19 of 21

Thanks! I got it to work!

Subject: Load Listbox from Array

From: Jussi Karlqvist

Date: 12 Oct, 2012 11:18:07

Message: 20 of 21

"GUIBuilder " <ericjaycorbett@gmail.com> wrote in message <ittggg$p9q$1@newscl01ah.mathworks.com>...
> Thanks! I got it to work!

I'm having the exact same problem. How do I have to modify the initialization code to get the handles struct created before the createFcns are called?

Subject: Load Listbox from Array

From: Joel

Date: 14 Jan, 2014 06:49:07

Message: 21 of 21

"Jussi Karlqvist" wrote in message <k58u9f$7t4$1@newscl01ah.mathworks.com>...
> "GUIBuilder " <ericjaycorbett@gmail.com> wrote in message <ittggg$p9q$1@newscl01ah.mathworks.com>...
> > Thanks! I got it to work!
>
> I'm having the exact same problem. How do I have to modify the initialization code to get the handles struct created before the createFcns are called?

Did you solve this problem? I too am having the same problem...getting an empty listbox after passing the names of files in a folder in the listbox1_CreateFcn() while executing it...

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us