四、Matlab 之 常見(jiàn)函數(shù)的使用
1、數(shù)據(jù)處理:
保存數(shù)據(jù)到本地并加載數(shù)據(jù)到matlab方法
① mat格式的讀取
保存
clear; a = magic(3); save myfata1.mat %save mydata2.mat -ascii %這個(gè)可以用記事本打開(kāi)
1
2
3
4
加載
clear; load('mydata1.mat'); load('mydata2.mat','-ascii');
1
2
3
② excel格式的讀寫(xiě)
讀取
Score = xlsread('1.xlsx','B2:D4') %自己會(huì)濾除文字
1
2
寫(xiě)入
M = mean(mean')'; xlswrite('1.xlsx', M, 1, 'E2:E4'); %格式為 xlswrite(filename, variable, sheet, location); xlswrite('1.xlsx', {'Mean'}, 1, 'E1');
1
2
3
4
如何excel的文字呢?它會(huì)自動(dòng)濾除文字的。
[Score, Text] = xlsread('1.xlsx');
1
同樣,,,寫(xiě)回去再使用
xlswrite('1.xlsx', headers, 1, 'A1:D4'); xlswrite('1.xlsx', score, 1, 'B2:D4');
1
2
即可。。。
2、關(guān)于上面的mean函數(shù)
mean是求平均數(shù),但是注意是以列向量求的。
如果想求行向量的平均數(shù),需要先把發(fā)轉(zhuǎn)置然后再求。
3、文件操作(跟C語(yǔ)言大同小異,復(fù)習(xí)下。。。)
x = 0:pi/10:pi; y = sin(x); fid = fopen('sin.txt', 'w'); for i=1:11 fprintf(fid, '%5.3f %8.4f\n', x(i), y(i)); end fclose(fid); type sin.txt %查看這個(gè)txt文檔的內(nèi)容
1
2
3
4
5
6
7
8
關(guān)于讀取,也再上個(gè)例程!
fid = fopen('1.text', 'r'); i = 1; while ~feof(fid) name(1,:) = fscanf(fid, '%5c', 1); year(i) = fscanf(fid, '%d', 1); No(i) = fscanf(fid, '%d\n', 1); ......
1
2
3
4
5
6
7
4、reshape函數(shù)
按照順序?qū)⒃瓉?lái)單元數(shù)組的元素進(jìn)行重新放置,得到新的單元數(shù)組元素個(gè)數(shù)和原數(shù)組相同。
必須滿足R1C1 = R2C2
題目:
查找字符串中給定字符的位置和數(shù)目
function test() str = input('str = '); c = input('c = '); index = strfind(str, c); disp(index); disp(length(index));
1
2
3
4
5
6
輸入字符的首字母大寫(xiě),其他小寫(xiě)
function test() str = input('str = '); n = length(str); if (str(1)>'Z') str(1) = str(1) - 32; end for i=2:n if(str(i) < 'a') str(i) = str(i) + 32; end end disp(str);
1
2
3
4
5
6
7
8
9
10
11
12
matlab 數(shù)據(jù)結(jié)構(gòu)
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請(qǐng)聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請(qǐng)聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。