matlab和Python线性规划
2016-10-11 15:00
701 查看
先说matlab,数据用csv文件,
第一种: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])
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:
其他格式可以看帮助文档
Examples:
感谢: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的值
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
打开数据
用csvread函数
注意: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函数
在使用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
拟合
cftool
可以进行拟合,很方便的看到拟合效果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
相关文章推荐
- Python 与 Matlab混合语言编程资料
- 大家如果要看技术文章,请到我的收藏里面看『AutoCAD,Delphi,Java,Linux,Fortran,Python,Matlab』
- python中对matlab的支持库
- Python和Matlab
- Python库numpy中的svd分解和Matlab中的svd的一点区别
- FANN 自由开放源码神经网络库 支持Python, PHP, C++, .NET, Delphi, Matlab, Octave, Ruby, Pure Data, Mathematica
- Matlab 中的线性规划函数使用方法
- 科学计算:Python VS. MATLAB(4)----图形系统简介
- 如何在python中读写和存储matlab的数据文件(*.mat)
- 支持Python系列:Matlab的Python实现Scipy和Matplotlib简介
- PyMat - An interface between Python and MATLAB
- Lu与C/C++、Forcal、MATLAB、Python、Lua等各种语言的速度比较
- 用于科学计算的Python库-代替Matlab
- 运筹学——线性规划Matlab求解
- Matplotlib:仿Matlab的Python的2D图形库
- Matlab的Python实现Scipy和Matplotlib简介
- 用msscript.ocx连接Matlab与Python
- 科学计算:Python VS. MATLAB (1)----给我一个理由先
- python+numpy+scipy=matlab,抛弃matlab
- 线性规划问题的matlab求解