先说matlab,数据用csv文件,
注意:csvread函数只试用与用逗号分隔的纯数字文件
第一种:M = CSVREAD(‘FILENAME’) ,直接读取csv文件的数据,并返回给M
第二种:M = CSVREAD(‘FILENAME’,R,C) ,读取csv文件中从第R-1行,第C-1列的数据开始的数据,这对带有头文件说明的csv文件(如示波器等采集的文件)的读取是很重要的。
第三种:M = CSVREAD(‘FILENAME’,R,C,RNG),其中 RNG = [R1 C1 R2 C2],读取左上角为索引为(R1,C1) ,右下角索引为(R2,C2)的矩阵中的数据。
注意:matlab认为CSV第1行第1列的单元格坐标为(0,0)
例1.1 读取整个文件
csvread(‘csvlist.csv’)
例1.2 读取第2行以下,第0列以右区域的数据
m = csvread(‘csvlist.dat’, 2, 0)
例1.3 读取第2行以下,第0列以右,第3行以上,第3列以左区域的数据
m = csvread(‘csvlist.dat’, 2, 0, [2,0,3,3])
在使用textscan函数前必须用fopen函数打开CSV文件。textscan函数读取的结果会存在cell数组中。
help textscan
可以知道该函数可以通过控制参数,从文本文件或字符串文件中读取数据
C = textscan(FID,’FORMAT’) reads data from an open text file identified by FID into cell array C. Use FOPEN to open the file and obtain FID. The FORMAT is a string of conversion specifiers enclosed in single quotation marks(由单括号括起来的一串字符说明). The number of specifiers determines the number of cells in the cell array C. (说明字符的数量决定了输出cell的数量)For more information, see “Format Options.”
C = textscan ( FID, ‘FORMAT’ ,N) reads data from the file, using the FORMAT N times, where N is a positive integer. To read additional data from
the file after N cycles, call textscan again using the original FID.
C = textscan (FID, ‘FORMAT’, ‘PARAM’, VALUE) accepts one or more comma-separated parameter name/value pairs. For a list of parameters and values, see “Parameter Options.”
C = textscan(FID,’FORMAT’,N,’PARAM’,VALUE) reads data from the file, using the FORMAT N times, and using settings specified by pairs of PARAM/VALUE arguments.
C = textscan(STR,…) reads data from string STR. You can use the FORMAT, N, and PARAM/VALUE arguments described above with this syntax. However, for strings, repeated calls to textscan restart the scan from the beginning each time.(重复调用时每次从文件首部开始) (To restart a scan from the last position, request a POSITION output. See also Example 3.)
[C, POSITION] = textscan(…) returns the file or string position at the end of the scan as the second output argument. For a file, this is the value that FTELL(FID) would return after calling textscan(将读取位置作为第二个参数输出). For a string, POSITION indicates how many characters textscan read.(对于字符串而言,位置参数显示了以读取的字符数)
Supported values for SPECIFIER:
Numeric Input Type Specifier Output Class
------------------ --------- ------------
Integer, signed %d int32
%d8 int8
%d16 int16
%d32 int32
%d64 int64
Integer, unsigned %u uint32
%u8 uint8
%u16 uint16
%u32 uint32
%u64 uint64
Floating-point number %f double
%f32 single
%f64 double
%n double
其他格式可以看帮助文档
Examples:
Example 1: Read each column of a text file.
Suppose the text file 'mydata.dat' contains the following:
Sally Level1 12.34 45 1.23e10 inf Nan Yes 5.1+3i
Joe Level2 23.54 60 9e19 -inf 0.001 No 2.2-.5i
Bill Level3 34.90 12 2e5 10 100 No 3.1+.1i
Read the file:
fid = fopen('mydata.dat');
C = textscan(fid, '%s%s%f32%d8%u%f%f%s%f');
fclose(fid);
textscan returns a 1-by-9 cell array C with the following cells:
C{1} = {'Sally','Joe','Bill'} %class cell
C{2} = {'Level1'; 'Level2'; 'Level3'} %class cell
C{3} = [12.34;23.54;34.9] %class single
C{4} = [45;60;12] %class int8
C{5} = [4294967295; 4294967295; 200000] %class uint32
C{6} = [Inf;-Inf;10] %class double
C{7} = [NaN;0.001;100] %class double
C{8} = {'Yes','No','No'} %class cell
C{9} = [5.1+3.0i; 2.2-0.5i; 3.1+0.1i] %class double
感谢:http://blog.sina.com.cn/s/blog_a0246c110101v1k3.html
可以进行拟合,很方便的看到拟合效果
1. 右键还能一键保存在底部的table of fits 中选中行右键,save … to Workspace
2. 也可以生成M文件,函数输出fitresult,和gofl两个量。其中fitresult为函数拟合的变量结果,用fitresult.参数名就可以查看参数。
3. 比如你想模拟 y = p(1)*x + p(2);
通过lsqcurvefit函数得到了 p(1)和p(2);
你把方程右边变成字符串类型: str = [num2str(p(1)),’*x+’,num2str(p(2))];
然后global y; y = inline(str);
然后再另一个控件的callback中,global y; 假设自变量x=1,输入y(1)就会得到因变量y的值
fit:用曲线或曲面拟合数据
fitobject = fit(x,y,fitType)按fittype说明的参数对x,y进行拟合
fitobject = fit([x,y],z,fitType)三元
fitobject = fit(x,y,fitType,fitOptions)按fitoptions设置的参数用fittype对xy进行拟合
fitobject = fit(x,y,fitType,Name,Value)
[fitobject,gof] = fit(x,y,fitType)
[fitobject,gof,output] = fit(x,y,fitType)
感谢:http://cn.mathworks.com/help/curvefit/fit.html?s_tid=gn_loc_drop