MATLAB学习笔记(三) 数据或变量的类型转换与存取 Data structure and File access
1数据或变量的类型转换
1 2 3 4 5 6 7 8 9 10
double() Convert to double precision single() Convert to single precision int8() Convert to 8-bit signed integer int16() Convert to 16-bit signed integer int32() Convert to 32-bit signed integer int64() Convert to 64-bit signed integer uint8() Convert to 8-bit unsigned integer uint16() Convert to 16-bit unsigned integer uint32() Convert to 32-bit unsigned integer uint64() Convert to 64-bit unsigned integer
cell2struct Convert cell array to structure array fieldnames Field names of structure, or public fields of object getfield Field of structure array isfield Determine whether input is structure array field isstruct Determine whether input is structure array orderfields Order fields of structure array rmfield Remove fields from structure setfield Assign values to structure array field struct Create structure array struct2cell Convert structure to cell array structfun Apply function to each field of scalar structure
A(1,1)={[143; 058; 729]}; A(1,2)={'Anne Smith'}; A(2,1)={3+7i}; A(2,2)={-pi:pi:pi}; A 或 A{1,1}=[143; 058; 729]; A{1,2}='Anne Smith'; A{2,1}=3+7i; A{2,2}=-pi:pi:pi; A
执行结果如下:
读取时需要使用大括号:
1 2
A{1,1} A{1,1}(3)
cell array functions:
1 2 3 4 5 6 7 8 9 10 11
cell Create cell array cell2mat Convert cell array to numeric array cell2struct Convert cell array to structure array celldisp Cell array contents cellfun Apply function to each cell in cell array cellplot Graphically display structure of cell array cellstr Create cell array of strings from character array iscell Determine whether input is cell array mat2cell Convert array to cell array with different sized cells num2cell Convert array to cell array with consistently sized cells struct2cell Convert structure to cell array
matrix与cell的转换:
1 2 3
a = magic(3) magic表示魔法矩阵,每行每列以及对角线之和相等 b = num2cell(a) c = mat2cell(a, [1 1 1], 3) 3表示将三个column放一起
A{1,1} = [12;45]; A{1,2} = 'Name'; A{2,1} = 2-4i; A{2,2} = 7; B{1,1} = 'Name2'; B{1,2} = 3; B{2,1} = 0:1:3; B{2,2} = [45]'; C = cat(3, A, B)
reshape
Returns a new array with assigned rows and columns
1 2
A = {'James Bond', [12;34;56]; pi, magic(5)} C = reshape(A,1,4) 将2x2矩阵转成1x4矩阵
1.8Checking
Variable And Variable Status检查变量类型
1 2 3 4 5 6 7 8 9 10 11 12 13 14
isinteger Determine if input is integer array islogical Determine if input is logical array isnan Detect an element that isnot a number (NaN) isnumeric Determine if input is numeric array isprime Detect prime elements of array isreal Determine if all array elements are real numbers iscell Determine if input is cell array ischar Determine if input is character array isempty Determine if input is empty array isequal Determine if arrays are numerically equal isfloat Determine if input is floating-point array isglobal Determine if input is global variable ishandle Detect valid graphics object handles isinf Detect infinite elements of array
2File access
2.1save
1 2 3 4 5
clear; a = magic(4); save mydata1.mat save mydata2.mat -ascii 存成ASCII可以用记事本打开
M = mean(Score')'; xlswrite('04Score.xlsx', M, 1, 'E2:E4'); 顺序分别是文件名、变量名、sheet、写入的位置 xlswrite('04Score.xlsx', {'Mean'}, 1, 'E1'); 将单元E1写成mean
2.5获取文本
1
[Score Header] = xlsread('04Score.xlsx')
2.6Low-level File I/O
Functions
1 2 3 4 5
fopen Open file, or obtain information about openfiles fclose Close oneor all openfiles fscanf Read data fromtextfile fprintf Write data totextfile feof Test forend-of-file
1
fid = fopen('[filename]', '[permission]');
Writing Sine Values into A File:
1 2 3 4 5
x = 0:pi/10:pi; y = sin(x); fid = fopen('sinx.txt','w'); fori=1:11 fprintf(fid,'%5.3f %8.4f\n', x(i), y(i)); end fclose(fid); type sinx.txt