Search code examples
matlabmatlab-gui

Add elements to uilistbox programmatically


I'm playing with MATLAB GUIs and I want to add elements to a listBox as my program generate them. I have a function that generates data and I want to put the "Name" of those data inside a list box. Here's my function:

function [ birdInfo, trackBuff ] = saveParabolaOnFramesPlot( birdInfo, trackBuff , f, listbox)

Here's how I actually set the element, but it fails with the following error:

There is no String property on the ListBox class
set(listbox, 'String', stringOfField)

The value of stringOfField is just a string.

Here's how I call this function from AppDesigner Code View:

[app.birdInfo, app.trackBuff ] = saveParabolaOnFramesPlot( app.birdInfo, app.trackBuff , app.birdInfo.aFrame, app.JumpListListBox);

How can I fix this?


Solution

  • 'String' is the property that is used by the uicontrol objects which are different than the ones created by AppDesigner. Based on the documentation for uilistbox, you'll want to set the Items property instead

    Also if you're wanting to append a new item, you'll want to get the current list of items (a cell array of strings) and append your new item before assigning it.

    currentItems = get(listbox, 'Items');
    newitems = cat(2, currentItems, stringOfField);
    set(listbox, 'Items', newitems)
    

    Or more simply:

    listboxt.Items{end+1} = stringOfField;