Modify Import Data Function
7 ビュー (過去 30 日間)
古いコメントを表示
Matlab generated the following Code for importing data from Excel
function Rohdaten37C = importfile1(workbookFile, sheetName, dataLines)
%IMPORTFILE1 Import data from a spreadsheet
% ROHDATEN37C = IMPORTFILE1(FILE) reads data from the first worksheet
% in the Microsoft Excel spreadsheet file named FILE. Returns the
% numeric data.
%
% ROHDATEN37C = IMPORTFILE1(FILE, SHEET) reads from the specified
% worksheet.
%
% ROHDATEN37C = IMPORTFILE1(FILE, SHEET, DATALINES) reads from the
% specified worksheet for the specified row interval(s). Specify
% DATALINES as a positive scalar integer or a N-by-2 array of positive
% scalar integers for dis-contiguous row intervals.
%
% Example:
% Rohdaten37C = importfile1("D:\Messreihe RL 3Hz 120us\Rohdaten37°C.xlsx", "Tabelle1", [2, 13574]);
%
% See also READTABLE.
%
% Auto-generated by MATLAB on 21-Feb-2021 14:03:36
%% Input handling
% If no sheet is specified, read first sheet
if nargin == 1 || isempty(sheetName)
sheetName = 1;
end
% If row start and end points are not specified, define defaults
if nargin <= 2
dataLines = [2, 13574];
end
%% Set up the Import Options and import the data
opts = spreadsheetImportOptions("NumVariables", 9);
% Specify sheet and range
opts.Sheet = sheetName;
opts.DataRange = "A" + dataLines(1, 1) + ":I" + dataLines(1, 2);
% Specify column names and types
opts.VariableNames = ["VarName1", "VarName2", "VarName3", "VarName4", "VarName5", "VarName6", "VarName7", "VarName8", "VarName9"];
opts.VariableTypes = ["double", "double", "double", "double", "double", "double", "double", "double", "double"];
% Import the data
Rohdaten37C = readtable(workbookFile, opts, "UseExcel", false);
for idx = 2:size(dataLines, 1)
opts.DataRange = "A" + dataLines(idx, 1) + ":I" + dataLines(idx, 2);
tb = readtable(workbookFile, opts, "UseExcel", false);
Rohdaten37C = [Rohdaten37C; tb]; %#ok<AGROW>
end
%% Convert to output type
Rohdaten37C = table2array(Rohdaten37C);
end
The problem is that this code has a predefined number of columns - see opts.DataRange, opts.VariableNames, opts.VariableTypes and the "9" in opts. I want to modify this code in a way such that this function is able to read an arbitrary number of columns, maybe with an additional input parameter n which gives the number of columns of the respective Excel file which the user want to read in. Is it somehow possible to accomplish this task?
Thanks
1 件のコメント
dpb
2021 年 2 月 21 日
編集済み: dpb
2021 年 2 月 21 日
It's possible to read a general Excel spreadsheet, but I'd never start with this code to do so...if the file(s) are regular but just different sizes, simply call readtable directly; it'll probably work without any other effort.
If you want the data in an array instead of a table, then call importdata directly on each file.
回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Spreadsheets についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!