Serious error in transferring strings from one listbox to another

조회 수: 2 (최근 30일)
William
William 2011년 6월 16일
댓글: Giorgi 2014년 11월 26일
[EDIT: 20110616 11:18 CDT - reformat - WDR]
I am trying to create two list boxes. One will list the contents of a .MAT file, the other will hold the selected variable files that I choose when I select the variables in the first list box and hit "Select" they will move into the other list box. I am trying to update the number of items in listbox2 and keep crashing matlab. here is the code:
% --- Executes on button press in select_button.
function select_button_Callback(hObject, eventdata, handles)
% hObject handle to select_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
list_entry = get(handles.listbox1,'String');
index_selected = get(handles.listbox1,'Value');
choice_listbox1 = list_entry(index_selected);
update_listbox2 = get(handles.listbox2, 'string');
newmenu = [choice_listbox1, update_listbox2];
% for newmenulist = 1:length(newmenu)
% listofvars2 = [listofvars2 )];
% end
set(handles.listbox2,'String', newmenu);
If I have three variables and add the first two nothing happens but adding the third crashes it. I have no idea why.
Thank you for your help
Bill

채택된 답변

Fangjun Jiang
Fangjun Jiang 2011년 6월 16일
The easiest thing to do is to put a break point in your code and step it through to see where and when the error happened. I suspect the line newmenu = [choice_listbox1, update_listbox2] has problem with data type, string or cell. Pay attention to that line. Once you know the line where error happens, the next time you can pause there, copy and run the line at the command window to see the error message. That way, the error will not cause your debugging to stop.
  댓글 수: 1
William
William 2011년 6월 16일
I did that but I cannot get any good info out of the error. Here it is
??? Error using ==> horzcat
CAT arguments dimensions are not consistent.
Error in ==> MAT_to_Csv>select_button_Callback at 238
newmenu = [choice_listbox1, update_listbox2];
Error in ==> gui_mainfcn at 96
feval(varargin{:});
Error in ==> MAT_to_Csv at 42
gui_mainfcn(gui_State, varargin{:});
??? Error while evaluating uicontrol Callback

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

추가 답변 (2개)

Walter Roberson
Walter Roberson 2011년 6월 16일
% --- Executes on button press in select_button.
function select_button_Callback(hObject, eventdata, handles)
% hObject handle to select_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
list_entry = cellstr(get(handles.listbox1,'String'));
index_selected = get(handles.listbox1,'Value');
choice_listbox1 = list_entry(index_selected);
update_listbox2 = cellstr(get(handles.listbox2, 'string'));
newmenu = [choice_listbox1, update_listbox2];
set(handles.listbox2,'String', newmenu);
That is, you are likely having a conflict with listbox String defaulting to '' (empty string) when you want to be working with cells. Also, some ways of initializing listbox string can result in char arrays rather than cell arrays. It is safer to cellstr() to be sure you have cell arrays.
Question: your code puts the newly selected variable at the top of the list. Is that what you want?

William
William 2011년 6월 16일
I fixed it! Thanks
  댓글 수: 4
Giorgi
Giorgi 2014년 11월 26일
Hi guys! Thats perfect that you fixed it buut i have the same problem so please if you can explain the explanations in details.
Giorgi
Giorgi 2014년 11월 26일
OH good i fixed it !

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

카테고리

Help CenterFile Exchange에서 Entering Commands에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by