MATLAB03: data type and file reading and writing
The best way to learn a technology is to read the official documents. You can view the official documents of MATLAB
data type
The main data types in MATLAB are as follows:
The following describes the main data types in turn, MATLAB official document Describes all data types
Numeric type
In MATLAB, the variable of numerical type is double by default, and can be converted to other numerical types by type conversion
n = 3; class(n) % obtain double n = int8(3); class(n) % obtain int8
The numerical types supported by MATLAB are shown in the following table:
value type | describe |
---|---|
double | Double precision floating point number |
single | Single-precision floating-point |
int8 | 8-bit signed integer |
int16 | 16 bit signed integer |
int32 | 32-bit signed integer |
int64 | 64 bit signed integer |
uint8 | 8-bit unsigned integer |
uint16 | 16 bit unsigned integer |
uint32 | 32-bit unsigned integer |
uint64 | 64 bit unsigned integer |
String type (char)
In MATLAB, the string type is defined by a pair of single quotation marks' wrapping a text Standard ASCII characters can be converted to corresponding ASCII codes
s1 = 'h'; uint16(s1) % Get 104
Strings are stored in memory in the form of character matrix, which can be indexed and assigned:
str1 = 'hello'; str2 = 'world'; str3 = [str1 str2]; size(str3) % obtain [1 10] str4 = [str1; str2]; size(str4) % obtain [2 5]
str = 'aardvark'; 'a' == str % obtain [1 1 0 0 0 1 0 0] Perform equivalent operation at each position str(str == 'a') = 'Z' % obtain 'ZZrdvZrk' it turns out to be the case that a All become Z
Structure
In MATLAB, structure is a data structure that stores {key: value}, which is similar to the dictionary in Python language
Basic use of structure
- Similar to most programming languages, MATLAB uses To access fields in the structure:
%% Create structural questions student.name = 'John Doe'; student.id = 'jdo2@sfu.ca'; student.number = 301073268; student.grade = [100, 75, 73; ... 95, 91, 85.5; ... 100, 98, 72]; % Direct input student View structure student % Direct input student.grade View details student.grade %% filenames(student) Structure names can be listed (specific table below) >>filenames(student) ans = 4×1 cell array {'name' } {'id' } {'number'} {'grade' }
- Using subscript expressions on the structure list can expand or reduce the structure list
student(2).name = 'Ann Lane'; student(2).id = 'aln4@sfu.ca'; student(2).number = 301078853; student(2).grade = [95 100 90; 95 82 97; 100 85 100]; student student(1) = [] % delete student First item in the list
- Structures can be cascaded, that is, the value of fields in structures can also be structures:
A = struct('data', [3 4 7; 8 0 1], ... 'nest', struct('testnum', 'Test 1', ... 'xdata', [4 2 8], ... 'ydata', [7 1 6])); A(2).data = [9 3 2; 7 6 5]; A(2).nest.testnum = 'Test 2'; A(2).nest.xdata = [3 4 2]; A(2).nest.ydata = [5 0 9];
Common functions of structures
Function function
function | effect |
---|---|
struct | Create structure |
struct2cell | Convert structure to cell array |
cell2struct | Convert a cell array to a structure |
isstruct | Judge whether a variable is a structure |
structfun | Apply a function to each field of the structure |
fieldnames | Gets all field names of the structure |
isfield | Determine whether the structure contains a field |
getfield | Gets the value of a field in the structure |
setfield | Assign a value to a field in the structure |
rmfield | Delete a field in a structure |
orderfields | Sort structure fields |
1.4.1 basic use of cell array
We can use {} to define a cell array like a matrix:
A = { [1 4 3; 0 5 8; 7 2 9] 'Anne Smith' ;... 3+7i -pi:pi:pi}
A(1,1)={[1 4 3; 0 5 8; 7 2 9]}; A(1,2)={'Anne Smith'}; A(2,1)={3+7i}; A(2,2)={-pi:pi:pi}; A
A{1,1}=[1 4 3; 0 5 8; 7 2 9]; A{1,2}='Anne Smith'; A{2,1}=3+7i; A{2,2}=-pi:pi:pi; A
The above three methods are equivalent, in which the second method uses unit index assignment and the third method uses content index assignment
- There are two ways to access data in a cell array: the cell index () and the content index {}
Because the subset of cell array is still cell array, in the use of indexer content, it is necessary to indicate whether we want to access a sub cell array or the content in the corresponding area of cell array
- Using the cell index (), we get an array of sub cells
- Using the content index {}, we get the content in the corresponding area of the cell array
For the difference between unit index and content index, please refer to the official documents
Common functions of cell array
Function function
function | effect |
---|---|
cell | Create a cell array |
iscell | Determine whether a variable is a cell array |
cell2mat | Convert cell array to matrix |
cell2struct | Convert cell array to structure |
mat2cell | Converts an array to a cell array of the specified size |
num2cell | Converts an array to a cell array of the same size |
struct2cell | Convert structure to cell array |
celldisp | Recursively displays the contents of a cell array |
cellplot | Draw the structure of cell array in image form |
cellfun | Apply a function to each cell of the cell array |
a = magic(3) b = num2cell(a) % obtain % [8] [1] [6] % [3] [5] [7] % [4] [9] [2] c = mat2cell(a, [1 2], [2, 1]) % obtain % [1x2 double] [6] % [2x2 double] [2x1 double]
High dimensional cell array
A three-dimensional cell array can have three dimensions: row, column and layer When indexing cellular arrays, the order of priority from high to low is: row → column → layer
Using cat function, you can splice the cell array on the specified dimension
Function for judging variable data type
The following functions can judge the variable type:
Function function
function | effect |
---|---|
isinteger | Judge whether the input parameter is an integer array |
islogical | Judge whether the input parameter is a logical quantity array |
isnumeric | Judge whether the input parameter is a numeric array |
isreal | Determine whether the input parameter is a real array |
ischar | Judge whether the input parameter is a character array |
iscell | Judge whether the input parameter is a cell array |
isfloat | Determine whether the input array is a floating-point array |
ishandle | Determines whether the input array is a valid graphic handle |
isempty | Determine whether the input array is empty |
isprime | Determines which array elements are prime |
isnan | Determines which array elements are NaN |
isinf | Determines which array elements are Inf |
isequal | Determine whether the arrays are equal |
File content extension function to read file function to write file
File content | Extension | Functions to read files | Function to write to file |
---|---|---|---|
MATLAB data | *.mat | load | save |
Excel table | *.xls,*.xlsx | xlsread | xlswrite |
Space delimited numbers | *.txt | load | save |
- The syntax of the save function is as follows:
- save(filename,variables) saves the variables into a file in binary form
- save(filename,variables, '- ascii') saves the variables to a file as text
- The syntax of the load function is as follows:
- load(filename) reads data from a binary file
- load(filename, '- ascii') reads data from a text file
The parameters filename and variables are in string format. If the variables parameter is not specified, all variables in the current workspace will be stored in the file
Complex data formats, such as struct and cell, do not support storage in binary format
Reading and writing Excel tables
Excel data can be read and written using xlsread and xlswrite functions. The syntax is as follows:
- Syntax for reading Excel files: [num,txt,raw] = xlsread(filename,sheet,xlRange)
Score = xlsread('04Score.xlsx') Score = xlsread('04Score.xlsx', 'B2:D4') [Score Header] = xlsread('04Score.xlsx')
Syntax for writing Excel: xlswrite(filename,A,sheet,xlRange)
M = mean(Score)'; xlswrite('04Score.xlsx', M, 1, 'E2:E4'); xlswrite('04Score.xlsx', {'Mean'}, 1, 'E1');