Undefined function or variable 'detectImportOptions'.

12 ビュー (過去 30 日間)
Chamath Vithanawasam
Chamath Vithanawasam 2018 年 8 月 24 日
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 件のコメント
Stephen23
Stephen23 2018 年 8 月 24 日
編集済み: Stephen23 2018 年 8 月 24 日
"... how do I alter this code to use it on a Matlab before 2016b?"
Either:
  • 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 2018 年 8 月 25 日
編集済み: Chamath Vithanawasam 2018 年 8 月 25 日
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.

サインインしてコメントする。

採用された回答

Walter Roberson
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 .
  1 件のコメント
Chamath Vithanawasam
Chamath Vithanawasam 2018 年 8 月 27 日
Thanks a lot! This works.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Distribution Plots についてさらに検索

製品


リリース

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by