null matlab--数据读取 matlab--数据读取一 、将excel数据导入matlab一 、将excel数据导入matlab1.直接导入
在文件菜单中选择 file/import data,
按照提示进行操作至结束。(book1.xls)
>> Sheet1
Sheet1 =
1 2 3
4 5 62.xlsread函数导入2.xlsread函数导入[filename,pathname]=uigetfile('*.xls');
file=[pathname filename];
x=xlsread(file);运行上面命令后,出现select file to open
对话框选择book1.xls选择book1.xls>> x
x =
1 2 3
4 5 6其他格式:其他格式:num = xlsread(filename)
num = xlsread(filename, -1)
num = xlsread(filename, sheet)
num = xlsread(filename, 'range')
num = xlsread(filename, sheet, 'range')
num = xlsread(filename, sheet, 'range', 'basic')
num = xlsread(filename, ..., functionhandle)
[num, txt]= xlsread(filename, ...)
[num, txt, raw] = xlsread(filename, ...)
[num, txt, raw, X] = xlsread(filename, ..., functionhandle)null>> A = xlsread('book1.xls', 1, 'E11:E12')
A =
1
4二、将matlab数据写入excel二、将matlab数据写入excelxlswrite(filename, M) 将矩阵M写入filename,
M的行数不能超过65536
M的列数不能超过256
在filename的sheet1中,从A1开始写入。>> a=magic(5);
>> xlswrite('book2.xls',a)其他格式其他格式xlswrite(filename, M, sheet)
xlswrite(filename, M, range)
xlswrite(filename, M, sheet, range)
status = xlswrite(filename, ...)
[status, message] = xlswrite(filename, ...)>> xlswrite('book3', a, 'shumo', 'E1')将矩阵a写入book3.xls的shumo工作
簿,从E1开始写入。null>>D = xlsread('Btest.xls', 1, 'B2:F5001');>> xlswrite('book4.xls',D)三、 将txt文件数据导入 matlab三、 将txt文件数据导入 matlabtest1.txt 的文件
如下
“ 你好,我的数据 坚持就会成功!
We can try! 1 11 111 1111 2 22 222 2222 3 33 333 3333 4 44 444 4444 5 55 555 5555 "
方法在文件菜单中选择 file/import data,按
照提示进行操作至结束。 >> data
data =
1 11 111 1111 2 22 222 2222 3 33 333 3333 4 44 444 4444 5 55 555 5555null>> textdata
textdata =
'你好,我的数据'
'坚持就会成功!'
'We can try!
null文件test2.txt内容:
你好 1 11 111 1111 坚持就会成功 2 22 222 2222 We can try! 3 33 333 3333 www.dytrol.com 4 44 444 4444 5 55 555 5555
说明:这种内容格式的文件用上面的方法是不行的。importtxt.mimporttxt.m fidin=fopen('test2.txt'); % 打开test2.txt文件
fidout=fopen('mkmatlab.txt','w');
% 创建MKMATLAB.txt文件
while ~feof(fidin) % 判断是否为文件末尾
tline=fgetl(fidin); % 从文件读行
if double(tline(1))>=48 & double(tline(1))<=57
% 判断首字符是否是数值
fprintf(fidout,'%s\n\n',tline);
% 如果是数字行,把此行数据写入文件MKMATLAB.txt
continue % 如果是非数字继续下一次循环
end
end
fclose(fidout);
mk=importdata('MKMATLAB.txt');
% 将生成的MKMATLAB.txt文件导入工作环境null>> MK
MK =
1 11 111 1111 2 22 222 2222 3 33 333 3333 4 44 444 4444 5 55 555 5555四、将matlab数据写入txt文件四、将matlab数据写入txt文件dlmwrite(filename, M)
dlmwrite(filename, M, 'D')
dlmwrite(filename, M, 'D', R, C)
dlmwrite(filename, M, 'attrib1', value1, 'attrib2', value2, ...)
dlmwrite(filename, M, '-append')
dlmwrite(filename, M, '-append', attribute-value list)null>> a=magic(5);
>> dlmwrite('test3.txt',a)
>> !type test3.txt
17,24,1,8,15
23,5,7,14,16
4,6,13,20,22
10,12,19,21,3
11,18,25,2,9
null>> dlmwrite('test4.txt',a,'delimiter',‘ ')
>> !type test4.txt
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9null>> M = magic(3);
>> dlmwrite('test5.txt', [M*5 M/5], ' ')>> !type test5.txt40 5 30 1.6 0.2 1.2
15 25 35 0.6 1 1.4
20 45 10 0.8 1.8 0.4null>> dlmwrite('test5.txt', rand(3), ...
'-append', 'roffset', 1, 'delimiter', ' ')>> !type test5.txt40 5 30 1.6 0.2 1.2
15 25 35 0.6 1 1.4
20 45 10 0.8 1.8 0.4
0.81472 0.91338 0.2785
0.90579 0.63236 0.54688
0.12699 0.09754 0.95751null>> dlmread('test5.txt')ans =
40.0000 5.0000 30.0000 1.6000 0.2000 1.2000
15.0000 25.0000 35.0000 0.6000 1.0000 1.4000
20.0000 45.0000 10.0000 0.8000 1.8000 0.4000
0.8147 0.9134 0.2785 0 0 0
0.9058 0.6324 0.5469 0 0 0
0.1270 0.0975 0.9575 0 0 0四、matlab数据文件四、matlab数据文件1.可以将工作空间中的变量用菜单save保存
成.mat文件;
2.可以用whos –file 加上文件名查看所存
变量;
3.导入.mat文件中数据用 load 命令。
例 如果将工作区间中变量存到testdatasave.mat文件中
>> whos -file testdatasave
>> load testdatasave