Download presentation
Presentation is loading. Please wait.
1
funCTIONs and Data Import/Export
CSCI N317 Computation for Scientific Applications Unit MATLAB funCTIONs and Data Import/Export
2
Outline Common built-in functions available to use
User defined functions Data import/export
3
Common Built-in Functions
Function revisit
4
Common Built-in Functions
Find a list of MATLAB commands and functions Type helpwin at the command line Click on the Help button
5
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.
6
User Defined Functions
7
Return Values A function doesn’t have to return values
8
Return Values A function can return more than one values
Return values are specified in square brackets separated by comma or space
9
Return Values Function output can be vectors.
10
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).
11
Local Variables vs Global Variables
variables defined outside the function definition Available both in function and workspace.
12
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.
13
Importing and Exporting Data
Read Excel spreadsheet files
14
Importing and Exporting Data
Read Excel spreadsheet files
15
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. Table: Arrays in tabular form whose named columns can have different types, can access data using array index or row/column name.
16
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
17
Importing and Exporting Data
Write a data into a Excel spreadsheet file
18
Importing and Exporting Data
Binary format, the file has an extension .mat, to be opened by MATLAB
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.