MATLAB Answers

Undefined function or variable 'detectImportOptions'.

62 views (last 30 days)
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;
handles.alldatatable = [handles.alldatatable; table1];
disp('Ready to use');
str = {};
set(handles.listbox1,'String',str); %set string
%evalin('base','clear all');
listString = get(handles.listbox3,'String');
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?


Stephen Cobeldick
Stephen Cobeldick on 24 Aug 2018
"... how do I alter this code to use it on a Matlab before 2016b?"
  • Write all of the required functions yourself (not trivial).
  • Rewrite your code to use the features provided with earlier MATLAB versions.
Chamath Vithanawasam
Chamath Vithanawasam on 25 Aug 2018
If I am to rewrite my code, how can I go about collecting data from the .txt file? Because the data is arranged in 144 columns, along with a date/time column as well. I need all that data in one table.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 25 Aug 2018
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);
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 .

  1 Comment

Sign in to comment.

More Answers (0)

Sign in to answer this question.