readtable
create a table from a file
Syntax
t = readtable(filename) t = readtable(filename, opts) t = readtable(filename, OptionName, Value, ...) t = readtable(filename, opts, OptionName, Value, ...)
Arguments
- filename
path or name of file to read. Accepted formats: ".xslx", ".csv", ".txt" and ".dat".
- opts
file import options obtained by detectImportOptions
Optional pairs OptionName, Value are:
- "VariableNames", vector of strings
extracts from the file only the data corresponding to the entered variable names.
- "ReadRowNames", boolean
the first column of the file will be stored in the RowNames property of the table t. Default value: %f.
- "ReadRowNames", boolean
the first column of the file will be stored in the RowNames property of the table t. Default value: %f.
- "Sheet", a string (sheet_name) or positive integer (sheet_index)
specify which sheet to read. Available option only is for Excel files. Default value: first sheet (index 1).
- "Range", string or double
A string or 2x2 matrix specifying the range of cells to read.
String formats: "A1:C10" (cell range), "A:C" (column range), "1:3" (row range), "B5" (first cell), "B" (first column), "3" (first row).
Matrix format: [row1 col1; row2 col2] where all values are positive integers >= 1.
Default value: "".
Available option only is for Excel files.
- t
output argument - table object.
Description
The readtable function creates a table from a file. Each column of file is stored in variables. If however the columns have no name, then the default variable names are used (["Var1", ..., "VarN"]). Accepted file formats are .txt, .dat, .csv and .xlsx.
readtable detects the format file thanks to detectImportOptions function. opts contains all information on the file.
To extract only the necessary variables (columns), use t = readtable(filename, "VariableNames", value).
t = readtable(filename, ..., "ReadRowNames", val) creates a table with row names, i.e, the first column of file is stored in RowNames property of t.
Examples
t = readtable(filename)
Code = ["AF"; "NA"; "OC"; "AN"; "AS"; "EU"; "SA"]; NameContinent = ["Africa"; "North America"; "Oceania"; "Antarctica"; "Asia"; "Europe"; "South America"]; Area = [30065000; 24256000; 7687000; 13209000; 44579000; 9938000; 17819000]; // in km2 NumberCountry = [54; 23; 14; %nan; 47; 45; 12]; LifeExpectancy = [60; 78; 75; %nan; 72; 75; 74]; // in years t = table(Code, NameContinent, Area, NumberCountry, LifeExpectancy, ... "VariableNames", ["Code", "NameContinent", "Area", "NumberCountry", "LifeExpectancy"]) // Write the table in CSV file writetable(t, fullfile(TMPDIR, "data.csv")) // Read the CSV file with readtable r = readtable(fullfile(TMPDIR, "data.csv"))
t = readtable(filename, "VariableNames", value)
Code = ["AF"; "NA"; "OC"; "AN"; "AS"; "EU"; "SA"]; NameContinent = ["Africa"; "North America"; "Oceania"; "Antarctica"; "Asia"; "Europe"; "South America"]; Area = [30065000; 24256000; 7687000; 13209000; 44579000; 9938000; 17819000]; // in km2 NumberCountry = [54; 23; 14; %nan; 47; 45; 12]; LifeExpectancy = [60; 78; 75; %nan; 72; 75; 74]; // in years t = table(Code, NameContinent, Area, NumberCountry, LifeExpectancy, ... "VariableNames", ["Code", "NameContinent", "Area", "NumberCountry", "LifeExpectancy"]) // Write the table in CSV file writetable(t, fullfile(TMPDIR, "data.csv")) // Read the CSV file with readtable r = readtable(fullfile(TMPDIR, "data.csv"), "VariableNames", ["NameContinent", "NumberCountry", "Area"])
t = readtable(filename, "ReadRowNames", value)
Code = ["AF"; "NA"; "OC"; "AN"; "AS"; "EU"; "SA"]; NameContinent = ["Africa"; "North America"; "Oceania"; "Antarctica"; "Asia"; "Europe"; "South America"]; Area = [30065000; 24256000; 7687000; 13209000; 44579000; 9938000; 17819000]; // in km2 NumberCountry = [54; 23; 14; %nan; 47; 45; 12]; LifeExpectancy = [60; 78; 75; %nan; 72; 75; 74]; // in years t = table(Code, NameContinent, Area, NumberCountry, LifeExpectancy, ... "VariableNames", ["Code", "NameContinent", "Area", "NumberCountry", "LifeExpectancy"]) // Write the table in CSV file writetable(t, fullfile(TMPDIR, "data.csv")) // Read the CSV file with readtable r = readtable(fullfile(TMPDIR, "data.csv"), "ReadRowNames", %t) r.Properties.RowNames
t = readtable(filename, "Sheet", value)
dt = datetime() + hours(0:4)'; double = [12.5; 8; 0.75; 53.26; 8.42]; bool = [%t; %f; %t; %f; %t]; str = "A" + string(0:4)'; t = table(dt, double, bool, str, "VariableNames", ["datetime","double", "bool", "string"]) // datetime double bool string // _______________________ ______ ____ ______ // // 2026-03-27 12:02:13.297 12.5 T A0 // 2026-03-27 13:02:13.297 8 F A1 // 2026-03-27 14:02:13.297 0.75 T A2 // 2026-03-27 15:02:13.297 53.26 F A3 // 2026-03-27 16:02:13.297 8.42 T A4 path = fullfile(TMPDIR, "table.xlsx"); xlsxSheet(path, "create", "tableSheet"); // create tableSheet writetable(t, path, "sheet", "tableSheet"); // write table in tableSheet t2 = readtable(path, "sheet", "tableSheet") // t2 = [5x4 table] // datetime double bool string // _______________________ ______ ____ ______ // // 2026-03-27 12:02:13.297 12.5 T A0 // 2026-03-27 13:02:13.297 8 F A1 // 2026-03-27 14:02:13.297 0.75 T A2 // 2026-03-27 15:02:13.297 53.26 F A3 // 2026-03-27 16:02:13.297 8.42 T A4
See also
- writetable — write a table to file
- table — create a table from variables
History
| バージョン | 記述 |
| 2024.0.0 | Introduction in Scilab. |
| 2026.1.0 | Empty columns are now preserved.
Previously, columns with missing or undefined names were not returned.
Similarly, columns with a defined name but containing no data were ignored.
As a result, the returned |
| 2026.1.0 |
|
| Report an issue | ||
| << pivot | Timeseries/Table | readtimeseries >> |