导出matlab程序,Matlab数据导入导出
Matlab提供了從磁盤文件或剪貼簿轉(zhuǎn)載數(shù)據(jù)至工作區(qū)(數(shù)據(jù)導(dǎo)入)和將工作區(qū)變量存入磁盤文件(數(shù)據(jù)導(dǎo)出)的多種途徑。
最簡單的辦法是使用界面導(dǎo)入向?qū)?#xff0c;打開文件菜單中的導(dǎo)入數(shù)據(jù)而后按提示操作。
一、導(dǎo)入文本文件
load函數(shù)、dlmread函數(shù)
文本文件需要具備統(tǒng)一的行列模式,使用分隔符作為數(shù)據(jù)項(xiàng)間隔,這些分隔符包括空格、逗號(hào)、tab、分號(hào)或其它。數(shù)據(jù)文件可能附帶標(biāo)題行和行列頭標(biāo)簽。
數(shù)值數(shù)據(jù)
對(duì)于數(shù)值數(shù)據(jù)可以直接使用load函數(shù)裝載,例如my_data.txt中數(shù)據(jù)如下:
1 2 3 4 5
6 7 8 9 10
命令A(yù) = load('my_data.txt')裝載該文本文件數(shù)據(jù)。
如果數(shù)值數(shù)據(jù)使用其它分隔符,可以使用dlmread讀入,假設(shè)my_data.txt中數(shù)據(jù)如下:
7.2;8.5;6.2;6.6
5.4;9.2;8.1;7.2
命令A(yù) = dlmread('my_data.txt', ';')讀入該數(shù)據(jù)。
包含行列標(biāo)簽的數(shù)值數(shù)據(jù)
例如:
Grade1 Grade2 Grade3
78.8 55.9 45.9
99.5 66.8 78.0
89.5 77.0 56.7
fid = fopen('grades.dat', 'r');
grades = textscan(fid, '%f %f %f', 3, 'headerlines', 1);
fclose(fid);
包含字符和數(shù)值的混合數(shù)據(jù)
使用textread函數(shù)讀入。
如果是規(guī)則的用空格隔開的數(shù)據(jù),則采用data=textread(filename)格式調(diào)用,讀出的數(shù)據(jù)記錄在data矩陣中。
二、導(dǎo)出文本文件
save函數(shù)
A = [ 1 2 3 4 ; 5 6 7 8 ];
save my_data.out A –ASCII
dlmwrite函數(shù)
dlmwrite('my_data.out',A, ';')
三、MS-Excel電子表格文件
xlsinfo獲得文件信息
使用命令[type, sheets] = xlsfinfo(filename)返回文件類型type和工作表信息。如:[type, sheets] = xlsfinfo('tempdata.xls')
Xlswrite導(dǎo)出數(shù)據(jù)
d = {'Time', 'Temp'; 12 98; 13 99; 14 97}
命令xlswrite('tempdata.xls', d, 'Temperatures', 'E1')將單元格數(shù)組d的數(shù)據(jù)寫出至tempdata.xls文件,新建工作表'Temperatures',從該工作表的E1單元格開始寫入。
Xlsread讀入數(shù)據(jù)
ndata = xlsread('tempdata.xls', 'Temperatures')
[ndata, headertext] = xlsread('tempdata.xls', 'Temperatures')
底層文件輸入輸出函數(shù)
fclose?? 關(guān)閉文件
fopen?? 打開文件
fread?? 從文件中讀入二進(jìn)制數(shù)據(jù)
fwrite? 把二進(jìn)制數(shù)據(jù)寫入文件
fgetl?? 逐行從文件中讀取數(shù)據(jù)并放棄換行符
fgets?? 從文件中讀取行,保留換行符并把行作為字符串返回
fprintf? 把格式化數(shù)據(jù)寫入文件
fscanf? 從文件中讀取格式化數(shù)據(jù)
feof??? 測試文件是否結(jié)束
ferror?? 測試文件輸入輸出錯(cuò)誤信息
frewind 文件指針歸零
fseek?? 設(shè)置文件位置指針
ftell??? 獲取文件位置指針
sprintf? 把格式化數(shù)據(jù)寫入一個(gè)字符串
sscanf?? 使用格式控制讀取字符串
底層文件輸入輸出函數(shù)-->特殊函數(shù)
csvread?? 讀取逗號(hào)分隔格式的數(shù)據(jù)文件到矩陣
csvwrite?? 寫矩陣到逗號(hào)分隔格式的數(shù)據(jù)文件
dlmread?? 把一個(gè)ASCII限定文件(數(shù)據(jù)文件)讀入矩陣
dlmwrite?? 把矩陣寫入到ASCII限定文件(數(shù)據(jù)文件)
hdf?????? HDF接口??
imfinfo?? 返回圖形圖象文件的信息
imread??? 讀取圖象(到矩陣)
imwrite?? 寫入圖象
textread?? 從文本文件讀取格式化數(shù)據(jù)(important)
wk1read? 把Lotus123電子表格讀入矩陣
wk1write? 把矩陣寫入Lotus123wk1電子表格
xlsread?? 讀取excel表格
Example 1 — Reading Different Types of DataText file scan1.dat contains data in the followingform:
Sally? Level1 12.34 45 1.23e10 inf NaN Yes
Joe??? Level2 23.54 60 9e19 -inf 0.001 No
Bill?? Level3 34.90 12 2e5 10 100 No
Read each column into a variable:
fid = fopen('scan1.dat');
C = textscan(fid, '%s %s %f32 %d8 %u %f %f %s');
fclose(fid);
Note:Spaces between the conversion specifiers are shown only to make the example easier to read. They are not required.
textscan returns a 1-by-8 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
The first two elements of C{5} are the maximum values for a 32-bit unsigned integer, or intmax('uint32').
總結(jié)
以上是生活随笔為你收集整理的导出matlab程序,Matlab数据导入导出的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: matlab试用账号,免费试用MATLA
- 下一篇: matlab人脸追踪,求大神帮助我这个菜