Read part 1 here. We will discuss few more functions to deal with Excel files.
xlswrite ( ) – Write to Microsoft Excel spreadsheet file
xlswrite is a simple function to write data to an excel worksheet. The syntax is similar to the xlsread function
xlswrtie(‘filename’, A, ‘worksheet’, ‘range’)
The function write the values in the array A to the specified range of cells in the specified worksheet of the excel file. The array may be a numeric array, character array or a cell array depending on the data. The worksheet and range arguments are optional. If not specified, the data will be written to the first worksheet starting from A1.
Example:
No we will add marks of two more students to our grade file.
>> newstud = {342001, 'James', 12.5, 13, 20;342001, 'Anna', 12, 10.5, 16};
>> xlswrite('test.xlsx', newstud, 'A7:E8')
writetable( ) - Write table to file
wrtietable function by default write the content of martlab table to a text file in comma separated format. The file name will be automatically set if not specified.
writetable(T) is the basic syntax.
writetable(T, ‘filename’) writes the table T to the file as column-oriented data. The function automatically determines the file format based on the extension specified in the filename.
Optional arguments to select the work sheet and range is available when you write to an excel file.
Example:
We will write the contents of the table T from the previous example to the second sheet of the same file.
>> writetable(T,'test.xlsx','sheet',2)
The command will write the data to the Sheet2.
Importdata( ) - Load data from file
importdata is used to load data from a file to the workspace. The function will recognise the file type from the extension and will call appropriate procedure to load the data.
A = importdata(‘filename’) loads data into the structure A.
Example:
>> A = importdata('test.xlsx')
A =
data: [1x1 struct]
textdata: [1x1 struct]
colheaders: [1x1 struct]
To get the contents use the commands
A.data
A.textdata
A.colheaders
No comments:
Post a Comment