user5603723
user5603723

Reputation: 193

writing elements in matlab listbox

I am trying to write a list in a listbox.

code:

function listbox1_Callback(hObject, eventdata, handles)
% hObject    handle to listbox1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns listbox1 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from listbox1
error = getappdata(0, 'error_norm');
rows = size(error,1);
for i = 1:rows
  set(handles.listbox1,'string',strcat('filt_',num2str(i)));
  for j = 1:length(error)
      set(handles.listbox1,'string',strcat('sig_',num2str(i),'_',num2str(j)));
      for k = 1:length(error{j}.main)
          set(handles.listbox1,'string',strcat('seg_',num2str(i),'_',num2str(j),'_',num2str(k)));
      end
  end
end

Where error is a array of structure, this array contains filters, singals in these filters, segments of these signals. based on the number of all these components, i want to write the list. I want to write something like this in the listbox:

filt_1
sig_1_1
seg_1_1_1
seg_1_1_2
sig_1_2
seg_1_2_1
seg_1_2_2

But apparently, 'set' function overwrites the elements, so all i am getting is 1 element and the last element.

Any suggestion to how to overcome this problem will be appreciated.

Upvotes: 0

Views: 119

Answers (1)

Skogsv
Skogsv

Reputation: 490

Yes, since set always overwrites the string, it is better to firstl build the string and then pass it to set.

Example

% Sample data
rows=4;
error=cell(1,5);

for i=1:length(error)
    error{i}.main=rand(1,4);
end

% Build string
str={};
for i=1:rows
    str{end+1}=sprintf('filt_%i',i);
    for j=1:length(error)
        str{end+1}=sprintf('sig_%i_%i',i,j);
        for k=1:length(error{j}.main)
            str{end+1}=sprintf('seg_%i_%i_%i',i,j,k);
        end
    end
end

% Set data
set(handle.listbox1,'String', str);

Depending on the size of the final string it might be a good idea to preallocate str for performance.

Upvotes: 1

Related Questions