Execute files from listbox one by one

조회 수: 1 (최근 30일)
Ghenji
Ghenji 2018년 2월 12일
답변: Ghenji 2018년 3월 2일
GUI load some files using Pushbutton1 inside Listbox1. Now i have got another pushbutton2 to select all the files populated in listbox1. Now I would like to read all data from the files in listbox1 one by one. callback for pushbutton3 should be such that it should get all the filenames from _ listbox1_ and read it individually. Some how am not able to do it. Do i need callback for listbox1 as well?
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
[filename,pathname,filterindex] = uigetfile('*.xls;*.xlsx;*.xlsm;*.xlsb', 'Multiselect', 'on');
set(handles.listboxExcelfiles,'string',filename);
% --- Executes on selection change in listbox1
function listbox1_Callback(hObject, eventdata, handles)
% Hints: contents = cellstr(get(hObject,'String')) returns listboxDatasheets contents as cell array
% contents{get(hObject,'Value')} returns selected item from listboxDatasheets
% --- Executes on button press in pushbutton2
function pushbutton2_Callback(hObject, eventdata, handles)
select_all = numel(get(handles.listboxExcelfiles,'string'));
set(handles.listboxExcelfiles,'value',1:select_all);
% --- Executes on button press in pushbutton3
function pushbutton3_Callback(hObject, eventdata, handles)
filename = get(handles.listbox1,'string'));
len = length(filename);
for j = 1 : len
[num,txt,raw] = xlsread(filename,10,'J10:J34');
set(handles.listbox2, 'string', num);
end
Also there could be overwriting in listbox2. How can i exclude value if there is a repetition in listbox2
  댓글 수: 3
Ghenji
Ghenji 2018년 2월 12일
Thank you for your answer. I made few changes accordingly -
function pushbutton3_Callback(hObject, eventdata, handles)
listBoxitems = cellstr(get(handles.listbox1,'String'));
selectedstring = listBoxitems{get(handles.listbox1,'Value')};
len = length(selectedstring);
for j = 1 : len
[num,txt,raw] = xlsread(filename,10,'J10:J34');
set(handles.listbox2, 'string', num);
end
and am getting this error -
Error using Interface.000208DB_0000_0000_C000_000000000046/Open
Invoke Error, Dispatch Exception:
Source: Microsoft Excel
Description: Sorry, we couldn't find Copy of BMI260 WMR V07-V09 Samples rev1.xlsm. Is it possible it was moved,
renamed or deleted?
Help File: xlmain11.chm
Help Context ID: 0
Error in colordefined>pushbuttonExtract_Callback (line 157)
workbook = excel.Workbooks.Open(selectedstring);
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in colordefined (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)colordefined('pushbuttonExtract_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.
Ghenji
Ghenji 2018년 2월 12일
well I was able to get answer to this. Problem was undefined path for the files. Needed following changes -
function pushbutton1_Callback(hObject, eventdata, handles)
[filename,pathname,filterindex] = uigetfile('*.xls;*.xlsx;*.xlsm;*.xlsb', 'Multiselect', 'on');
fullname = fullfile(pathname, filename);
set(handles.listboxExcelfiles,'string',fullname);
function pushbutton3_Callback(hObject, eventdata, handles)
listBoxItems = cellstr(get(handles.listboxExcelfiles,'String'));
selectedstring = listBoxItems{get(handles.listboxExcelfiles,'Value')};

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

채택된 답변

Ghenji
Ghenji 2018년 3월 2일
well I was able to get answer to this. Problem was undefined path for the files. Needed following changes -
[filename,pathname,filterindex] = uigetfile('*.xls;*.xlsx;*.xlsm;*.xlsb', 'Multiselect', 'on');
fullname = fullfile(pathname, filename);
set(handles.listboxExcelfiles,'string',fullname);
function pushbutton3_Callback(hObject, eventdata, handles)
listBoxItems = cellstr(get(handles.listboxExcelfiles,'String'));
selectedstring = listBoxItems{get(handles.listboxExcelfiles,'Value')};

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by