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.

 채택된 답변

Walter Roberson
Walter Roberson 2016년 8월 4일

0 개 추천

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

댓글 수: 9

JA
JA 2016년 8월 4일
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 2016년 8월 4일
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 2016년 8월 4일
편집: Walter Roberson 2016년 8월 4일
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 2016년 8월 4일
thanks! you're a hero
JA
JA 2016년 8월 4일
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 2016년 8월 4일
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 2016년 8월 4일
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.
all_choices = get(hObject, 'String');
choice_idx = get(hObject, 'Value');
chosen_string = all_choices{choice_idx};
JA
JA 2016년 8월 4일
You are my savior today.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

태그

질문:

JA
2016년 8월 4일

댓글:

JA
2016년 8월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by