writetable
write a table to file
Syntax
writetable(t) writetable(t, filename) writetable(..., OptionName, Value, ...)
Arguments
- t
table
- filename
name or path of file. Accepted formats: ".xslx", ".csv", ".txt" and ".dat".
Optional pairs OptionName, Value are:
- "Delimiter", string
the column separator. Default value: ",".
- "WriteRowNames", boolean
when %t, writes row names in the file (first column). Default value: %f.
- "WriteVariableNames", boolean
when %t, writes variables names in the file (column names). Default value: %t.
- "sheet", string or positive integer
specifies which sheet to write to.
If string: if the sheet name does not exist,
writetablecreates a new sheet.If integer: select the sheet. If the index is greater than the number of sheets, then a new sheet is created with the name "Sheet_" + string(index).
Default: 1 (first sheet). Available option only is for Excel files.
- "range", string or positive integer
A string or 2x2 matrix specifying where to start writing data.
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: "". Available option only is for Excel files.
- "writemode", string
specifies the write mode:
"overwrite": Clears the sheet before writing data.
"append": Keeps existing data and adds new data at the specified range.
Default: data is written without clearing (preserves other cells). Available option only is for Excel files.
Description
The writetable function writes a table into a text file, where data are separated by comma. Accepted file formats are .txt, .dat, .csv and .xlsx.
writetable(t) writes table t into the file table.txt saved in TMPDIR.
writetable(t, filename, OptionName, Value) can be used to specify the column delimiter, to write the row names and the variable names into the file.
Examples
writetbale(t, filename) and writetbale(t, filename, 'WriteVariableNames', %f)
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 TXT file writetable(t, fullfile(TMPDIR, "data.txt")) // Read the TXT file with readtable r = readtable(fullfile(TMPDIR, "data.txt")) // Write the table in TXT file writetable(t, fullfile(TMPDIR, "data.txt"), "WriteVariableNames", %f) // Read the TXT file with readtable r = readtable(fullfile(TMPDIR, "data.txt"))
t = readtable(filename, "WriteRowNames", %t)
x = ["a"; "b"; "c"; "d"; "e"]; x1 = [1:5]'; x2 = 2.*[1:5]'; t = table(x1, x2, "VariableNames", ["x1", "x2"]); t.Row = x; writetable(t, fullfile(TMPDIR, "data.csv"), "WriteRowNames", %t); r = readtable(fullfile(TMPDIR, "data.csv"), "ReadRowNames", %t)
t = writetable(filename, "sheet", sheetName)
id = [1;2;3]; name = ["Martin"; "Mary"; "Alice"]; value = [10.5; 15.8; 20.3]; t = table(id, name, value, "VariableNames", ["ID", "NAME", "VALUE"]) // t = [3x3 table] // // ID NAME VALUE // ___ ______ _____ // // 1 Martin 10.5 // 2 Mary 15.8 // 3 Alice 20.3 path = fullfile(TMPDIR, "data.xlsx"); xlsxSheet(path, "create", "data"); // create "data" sheet writetable(t, path, "sheet", "data"); // write table in "data" sheet t2 = readtable(path, "sheet", "data") // t2 = [3x3 table] // // ID NAME VALUE // ___ ______ _____ // // 1 Martin 10.5 // 2 Mary 15.8 // 3 Alice 20.3 t3 = readtable(path, "sheet", "data", "range", [1 1;2 3]) // t3 = [1x3 table] // // ID NAME VALUE // ___ ______ _____ // // 1 Martin 10.5
History
| Versão | Descrição |
| 2024.0.0 | Introduction in Scilab. |
| 2026.1.0 |
|
| Report an issue | ||
| << varfun | Timeseries/Table | writetimeseries >> |