Reading csv or excel file with three headliners

4 ビュー (過去 30 日間)
Benju Baniya
Benju Baniya 2023 年 4 月 5 日
コメント済み: Benju Baniya 2023 年 4 月 5 日
Hello,
I have a csv file (which is a eddy pro output) attached with 3 headliners. I want to import the data with headliners and have treid using this code to import the fle but it doesn't work. I can skip the first headliner.
filepath= 'G:\Shared drives\Project_Crockett\QAQC_plots\EC_Benju\';
filename = "smartflux.csv";
opts = detectImportOptions(filename,'NumHeaderLines',3); % number of header lines which are to be ignored
opts.VariableNamesLine = 2; % row number which has variable names
opts.DataLine = 4; % row number from which the actual data starts
opts.VariableUnitsLine = 3; % row number which has units specified
tbl = readtable('TestFile.csv',opts)
I get this error "Error using detectImportOptions (line 432)
Unable to find or open 'smartflux.csv'. Check the path and filename or file permissions."
Please help me solve this issue. Thank you.

採用された回答

Walter Roberson
Walter Roberson 2023 年 4 月 5 日
filename = "smartflux.csv";
That is a basic filename without any directory information.
filepath= 'G:\Shared drives\Project_Crockett\QAQC_plots\EC_Benju\';
Tnat assigns a value to a variable. filepath is not any kind of reserved name in MATLAB, so it does not have any other result: a variable is assigned, nothing else happens.
opts = detectImportOptions(filename,'NumHeaderLines',3); % number of header lines which are to be ignored
You pass the basic filename to detectImportOptions . detectImportOptions is going to search for the file along the MATLAB path but in this case is not going to find it.
What you should be doing is
filepath= 'G:\Shared drives\Project_Crockett\QAQC_plots\EC_Benju\';
filename = fullfile(filepath, "smartflux.csv");
opts = detectImportOptions(filename,'NumHeaderLines',3); % number of header lines which are to be ignored
opts.VariableNamesLine = 2; % row number which has variable names
opts.DataLine = 4; % row number from which the actual data starts
opts.VariableUnitsLine = 3; % row number which has units specified
tbl = readtable(filename, opts); %NOTICE CHANGE HERE TOO
  1 件のコメント
Benju Baniya
Benju Baniya 2023 年 4 月 5 日
Thank you so much. This helped.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSpreadsheets についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by