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 . i want to write on list box something like this:
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, so all i am getting is only 1 element and the last element.
How do i make it write all of it, any help will be appreciated.

 Réponse acceptée

Walter Roberson
Walter Roberson le 4 Août 2016

0 votes

You need to create a cell array of strings first, and then set() that cell array as the String property of the handle.

9 commentaires

JA
JA le 4 Août 2016
problem with creating the cell array of strings was the indices, it was very confusing. Can you please demonstarte how to do that. with the for loop structure i have shown
JA
JA le 4 Août 2016
I tried something like this:
error = getappdata(0, 'error_norm'); rows = size(error,1);
for i = 1:length(rows)
filt{i} = ['filt_', num2str(i)];
for j = 1:length(error)
filt{i,i+j} = ['signal_',num2str(i),'_',num2str(j)]
for k = 1:length(error{j}.main)
filt{i,i+j,i+j+k} = ['segment_',num2str(i),'_',num2str(j),'_',num2str(k)]
end
end
end
set(handles.listbox1,'string',filt);
still doesn't work. any help will be appriciated
Walter Roberson
Walter Roberson le 4 Août 2016
Modifié(e) : Walter Roberson le 4 Août 2016
idx = 0;
for i = 1:length(rows)
idx = idx + 1;
filt{idx} = ['filt_', num2str(i)];
for j = 1:length(error)
idx = idx + 1;
filt{idx} = ['signal_',num2str(i),'_',num2str(j)];
for k = 1:length(error{j}.main)
idx = idx + 1;
filt{idx} = ['segment_',num2str(i),'_',num2str(j),'_',num2str(k)];
end
end
end
set(handles.listbox1, 'string', filt);
But I recommend you switch to using sprintf()
idx = 0;
for i = 1:length(rows)
idx = idx + 1;
filt{idx} = sprintf('filt_%d', i);
for j = 1:length(error)
idx = idx + 1;
filt{idx} = sprintf('signal_%d_%d', i, j);
for k = 1:length(error{j}.main)
idx = idx + 1;
filt{idx} = sprintf('segment_%d_%d_%d', i, j, k);
end
end
end
set(handles.listbox1, 'string', filt);
JA
JA le 4 Août 2016
thanks! you're a hero
JA
JA le 4 Août 2016
Just 1 question, I have wrote this code on 'listbox1' callback, so this only appears when i click on the listbox. where should i write the code so that the list appears when the GUI is opened?
Walter Roberson
Walter Roberson le 4 Août 2016
Put it in your OpenFcn for the figure, provided that you know the rows and error arrays by the time you open the GUI.
Alternately, if length(rows) and length(error) and all the length(error{j}.main) cannot change, you could run that code to set the listbox properties before you save the GUIDE figure, and then you would not need to set them dynamically.
JA
JA le 4 Août 2016
Thanks, it worked. Just 1 more question. how to get the string of selected item in the listbox. when i write'get(handles.listbox1,'string')'. i get all items in the listbox and when i do 'get(handles.listbox1, 'Value')' i am getting 1x1 double 1. not the selected item name, but just 1. how do i get the selected item name as a string.
Walter Roberson
Walter Roberson le 4 Août 2016
all_choices = get(hObject, 'String');
choice_idx = get(hObject, 'Value');
chosen_string = all_choices{choice_idx};
JA
JA le 4 Août 2016
You are my savior today.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Scripts dans Centre d'aide et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by