导出matlab程序,Matlab数据导入导出
Matlab提供了從磁盤文件或剪貼簿轉載數據至工作區(數據導入)和將工作區變量存入磁盤文件(數據導出)的多種途徑。
最簡單的辦法是使用界面導入向導,打開文件菜單中的導入數據而后按提示操作。
一、導入文本文件
load函數、dlmread函數
文本文件需要具備統一的行列模式,使用分隔符作為數據項間隔,這些分隔符包括空格、逗號、tab、分號或其它。數據文件可能附帶標題行和行列頭標簽。
數值數據
對于數值數據可以直接使用load函數裝載,例如my_data.txt中數據如下:
1 2 3 4 5
6 7 8 9 10
命令A = load('my_data.txt')裝載該文本文件數據。
如果數值數據使用其它分隔符,可以使用dlmread讀入,假設my_data.txt中數據如下:
7.2;8.5;6.2;6.6
5.4;9.2;8.1;7.2
命令A = dlmread('my_data.txt', ';')讀入該數據。
包含行列標簽的數值數據
例如:
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);
包含字符和數值的混合數據
使用textread函數讀入。
如果是規則的用空格隔開的數據,則采用data=textread(filename)格式調用,讀出的數據記錄在data矩陣中。
二、導出文本文件
save函數
A = [ 1 2 3 4 ; 5 6 7 8 ];
save my_data.out A –ASCII
dlmwrite函數
dlmwrite('my_data.out',A, ';')
三、MS-Excel電子表格文件
xlsinfo獲得文件信息
使用命令[type, sheets] = xlsfinfo(filename)返回文件類型type和工作表信息。如:[type, sheets] = xlsfinfo('tempdata.xls')
Xlswrite導出數據
d = {'Time', 'Temp'; 12 98; 13 99; 14 97}
命令xlswrite('tempdata.xls', d, 'Temperatures', 'E1')將單元格數組d的數據寫出至tempdata.xls文件,新建工作表'Temperatures',從該工作表的E1單元格開始寫入。
Xlsread讀入數據
ndata = xlsread('tempdata.xls', 'Temperatures')
[ndata, headertext] = xlsread('tempdata.xls', 'Temperatures')
底層文件輸入輸出函數
fclose?? 關閉文件
fopen?? 打開文件
fread?? 從文件中讀入二進制數據
fwrite? 把二進制數據寫入文件
fgetl?? 逐行從文件中讀取數據并放棄換行符
fgets?? 從文件中讀取行,保留換行符并把行作為字符串返回
fprintf? 把格式化數據寫入文件
fscanf? 從文件中讀取格式化數據
feof??? 測試文件是否結束
ferror?? 測試文件輸入輸出錯誤信息
frewind 文件指針歸零
fseek?? 設置文件位置指針
ftell??? 獲取文件位置指針
sprintf? 把格式化數據寫入一個字符串
sscanf?? 使用格式控制讀取字符串
底層文件輸入輸出函數-->特殊函數
csvread?? 讀取逗號分隔格式的數據文件到矩陣
csvwrite?? 寫矩陣到逗號分隔格式的數據文件
dlmread?? 把一個ASCII限定文件(數據文件)讀入矩陣
dlmwrite?? 把矩陣寫入到ASCII限定文件(數據文件)
hdf?????? HDF接口??
imfinfo?? 返回圖形圖象文件的信息
imread??? 讀取圖象(到矩陣)
imwrite?? 寫入圖象
textread?? 從文本文件讀取格式化數據(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').
總結
以上是生活随笔為你收集整理的导出matlab程序,Matlab数据导入导出的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: matlab试用账号,免费试用MATLA
- 下一篇: comsol计算数据导出matlab,c