matlab路径(string)操作及基本data读取保存

来源:互联网 发布:北京软件技术学院 编辑:程序博客网 时间:2024/06/05 12:37
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 1 ## build a matrix; Why is matrix ?    % one studentstudentScores = [90 99 98 85];% gammar math english moralrates         = [0.3 0.3 0.3 0.1];jiDian        = studentScores * rates';    % many studentsstudentScores = [90 99 98 85 % good style; a line with ';' is ok                 80 70 59 90                 72 89 65 99];% gammar math english moral             rates = [0.3 0.3 0.3 0.1];jiDians = studentScores * rates';    % size whos double() uint8() char() size(studentScores);whos('studentScores');%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 2 ## save : save()    % save studentScores and jiDians in ScoresOfClass1.matsave('ScoresOfClass1.mat', 'studentScores', 'jiDians');%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 3 ## load : load() sprinf() disp() fopen() fprintf()Scores = load('ScoresOfClass1.mat', 'studentScores');Scores = Scores.studentScores;JiDians = load('ScoresOfClass1.mat', 'jiDians');JiDians = JiDians.JiDians;    % displaydisp(sprintf('Part %d is done ! Have a rest !', 1));sprintf('Part %d is done ! Have a rest !', 1) % enough    % txt associated fprintftxtFile = fopen('log3.txt', 'w');name = 'xiaoming';fenshu = 2100;fprintf(txtFile, '%s score is %d; \n', name, fenshu);fprintf(txtFile, '%s score is %d; \n', 'xiaohong', 80);    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 4 ## load a mat with absolute path : size() whos() structdata = load('C:\matlab\picture.mat'); % absolute pathdata = load('picture.mat'); % relative pathsize(data);whos('data');    % structstudent.name = 'chenhui';student.class = 2;student.age = 18;whos('student');data = data.Img;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 5 ## plot max min find mean var std : figure() plot() subplot() bar()%                                       max() min() mean() var() std()%                                       find() subRow = data(1,:); % row vectorsubCol = data(:,1); % col vectorfigure(1); plot(subRow);figure(2); imshow(data, []);figure(3); subplot(1,2,1); plot(subRow); title('subRow');subplot(1,2,2); imshow(data, []); title('data');figure(1)A = 1:10;plot(A)B = 5:0.1:6;plot(B)plot(A)hold onplot(B)data2 = 1:10;figure(4); bar(data2);max(subRow); max(subCol);min(subRow);data2 = [4 5 9 6 10 12 52 45 99 10 0 25 56 ];find(data2==5);find(data2>=14);find('asdsxadsss'=='a');id = find(data2==10);    % think  max(data2(id(1):end));max(data2(id(end):end));mean(subRow);std(subRow);var(subRow);    % max of a 2-D matrixmax(max(data));A = [1 2 3; 4 5 6];mean(A);mean(A,1);mean(A,2);%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 6 ## functioncompute(data2);%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 7 ## load files way 1 : ls() strcat()filePath = 'C:\matlab';fileNames = ls(filePath); % all files    % strcat ls filePathOfMat = strcat(filePath, '\*.mat');fileNames = ls(filePathOfMat); % specific files[m,n] = size(fileNames);for i=1:m    subData = load(fileNames(1,:));    subData = subData.Img;    plot(subData(:,4));end%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 8 ## string manipulation : strcat() strrep() sprintf() path = 'C:\matlab';fullPath = strcat(path, '\A123.mat');fixPath = strrep(fullPath, 'A123', 'D_D');    % savefor i=1:10    str = spritnf('\%d.mat',i);    str_fullPath = strcat(path, str);    Result = xxxx;    save(str_fullPath, 'Result');end    % loadfor i=1:10    str = spritnf('\%d.mat',i);    str_fullPath = strcat(path, str);    data = load(str_fullPath, 'Result');    data = data.Result;end

原创粉丝点击