funCTIONs and Data Import/Export CSCI N317 Computation for Scientific Applications Unit 1 - 3 MATLAB funCTIONs and Data Import/Export
Outline Common built-in functions available to use User defined functions Data import/export
Common Built-in Functions Function revisit
Common Built-in Functions Find a list of MATLAB commands and functions Type helpwin at the command line Click on the Help button https://www.mathworks.com/help/matlab/functionlist.html
User Defined Functions You can create your own functions, e.g. y = f(x) A function has three components Function name and definition – f Function input – x Function output – y Function definition can be stored in a script file, called Function M-file. Function file must start with the key word “function”. Function name and the file name must be the same.
User Defined Functions
Return Values A function doesn’t have to return values
Return Values A function can return more than one values Return values are specified in square brackets separated by comma or space
Return Values Function output can be vectors.
Local Variables vs Global Variables variables defined inside the function definition available in the function only, thus cannot be accessed in the workspace (outside of the function).
Local Variables vs Global Variables variables defined outside the function definition Available both in function and workspace.
Importing and Exporting Data Use the load function - Data imported from the text file will be stored in a variable with the same name as the file; Can use the function form of the command to save to a different variable.
Importing and Exporting Data Read Excel spreadsheet files http://www.mathworks.com/help/matlab/ref/xlsread.html
Importing and Exporting Data Read Excel spreadsheet files
Importing and Exporting Data Use the GUI “Import Data” feature. Data can be imported as Column Vectors: Each column becomes a vector variable, data is not stored as a single variable. Matrix: Text data becomes NaN Cell Array: array that can contain values with mixed data types, can access data using array index. http://www.mathworks.com/help/matlab/cell-arrays.html Table: Arrays in tabular form whose named columns can have different types, can access data using array index or row/column name. http://www.mathworks.com/help/matlab/ref/table.html
Importing and Exporting Data You can save data in a MATLAB session into a file on hard disk. Text format in ASCII codes, to be opened by text editors
Importing and Exporting Data Write a data into a Excel spreadsheet file http://www.mathworks.com/help/matlab/import_export/exporting-to-excel-spreadsheets.html
Importing and Exporting Data Binary format, the file has an extension .mat, to be opened by MATLAB