- Write all of the required functions yourself (not trivial).
- Rewrite your code to use the features provided with earlier MATLAB versions.
Undefined function or variable 'detectImportOptions'.
조회 수: 14 (최근 30일)
이전 댓글 표시
I have a code which is supposed to collect data from multiple text files. (I will attach two of them here). The code used is as follows.
function GUI_for_log_OpeningFcn(hObject, eventdata, handles, varargin)
disp('Please wait');
f = dir('*.log');
handles.alldatatable = [];
for ii = 1:numel(f)
DayFile = f(ii).name;
opts = detectImportOptions(DayFile);
opts = setvartype(opts,1,'datetime');
opts = setvaropts(opts,1,'InputFormat','dd.MM.uuuu HH:mm:ss');
table1 = readtable(DayFile,opts);
if isempty(handles.alldatatable)
handles.alldatatable = table1;
else
handles.alldatatable = [handles.alldatatable; table1];
end
end
%disp(handles.alldatatable);
disp('Ready to use');
setappdata(0,'totData',handles.alldatatable);
str = {f.name};
set(handles.listbox1,'String',str); %set string
%evalin('base','clear all');
%evalin('base','clc');
listString = get(handles.listbox3,'String');
assignin('base','listString',listString);
This works fine on any Matlab which is after 2016b, but how do I alter this code to use it on a Matlab before 2016b? The error that comes if I use it in an older version is
Undefined function or variable 'detectImportOptions'.
Error in GUI_for_log_GUI_for_log_OpeningFcn (line 56)
opts = detectImportOptions(DayFile);
Error in gui_mainfcn (line 220)
feval(gui_State.gui_OpeningFcn, gui_hFigure, [], guidata(gui_hFigure), varargin{:});
Error in GUI_for_log (line 42)
gui_mainfcn(gui_State, varargin{:});
The issue is with 'detectImportOptions' which is not available in versions before 2016b. Is there an alternative function I can use instead of this function in, say, 2015a? Or perhaps is there a way to install a toolkit to use this code without alterations?
댓글 수: 2
채택된 답변
Walter Roberson
2018년 8월 25일
Tested code.
filename = 'SI010218.txt';
fid = fopen(filename, 'rt');
header_cell = textscan(fid, '%[^\n]', 1, 'HeaderLines', 12);
header_fields = regexp(header_cell{1}{1}, ';', 'split');
junk = fgetl(fid); %because of the ^\n the \n is still in the buffer
units_line = fgetl(fid);
units_fields = regexp(units_line, ';', 'split');
numfields = length(header_fields);
fmt_cell = repmat({'%f'}, 1, numfields);
fmt_cell{1} = '%[^;]'; %D format cannot handle embedded blanks
fmt_cell{100} = '%{HH:mm:ss}D';
fmt = [fmt_cell{:}];
data_cell = textscan(fid, fmt, 'delimiter', ';', 'CollectOutput', true);
fclose(fid);
data_cell2D = [num2cell( datetime( data_cell{1}, 'InputFormat', 'dd.MM.uuuu HH:mm:ss') ), ...
num2cell( data_cell{2} ), ... %numeric
num2cell( data_cell{3} - dateshift(data_cell{3}, 'start', 'day') ), ... %datetime, make it duration
num2cell( data_cell{4} ) ]; %numeric
adjusted_headers = matlab.lang.makeUniqueStrings( matlab.lang.makeValidName( header_fields ) );
table1 = cell2table(data_cell2D, 'VariableNames', adjusted_headers);
table1.Properties.VariableDescriptions = header_fields;
table1.Properties.VariableUnits = units_fields;
Your 100'th field is AptTmRmg specified as HH:mm:ss . Your posted code probably imports it as a character vector; I convert it into a duration .
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Distribution Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!