四、Matlab 之 常见函数的使用

举报
ReCclay 发表于 2022/02/22 01:16:17 2022/02/22
【摘要】 1、数据处理: 保存数据到本地并加载数据到matlab方法 ① mat格式的读取 保存 clear; a = magic(3); save myfata1.mat %save mydat...

1、数据处理:

保存数据到本地并加载数据到matlab方法

① mat格式的读取
保存

clear;
a = magic(3);
save myfata1.mat
%save mydata2.mat -ascii %这个可以用记事本打开
  
 
  • 1
  • 2
  • 3
  • 4

加载

clear;
load('mydata1.mat');
load('mydata2.mat','-ascii');
  
 
  • 1
  • 2
  • 3

② excel格式的读写

读取

 Score = xlsread('1.xlsx','B2:D4') %自己会滤除文字

  
 
  • 1
  • 2

写入

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的文字呢?它会自动滤除文字的。

[Score, Text] = xlsread('1.xlsx');
  
 
  • 1

同样,,,写回去再使用

xlswrite('1.xlsx', headers, 1, 'A1:D4');
xlswrite('1.xlsx', score, 1, 'B2:D4');
  
 
  • 1
  • 2

即可。。。

2、关于上面的mean函数
这里写图片描述

mean是求平均数,但是注意是以列向量求的。
如果想求行向量的平均数,需要先把发转置然后再求。

3、文件操作(跟C语言大同小异,复习下。。。)

 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 %查看这个txt文档的内容
  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

这里写图片描述

关于读取,也再上个例程!

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函数
按照顺序将原来单元数组的元素进行重新放置,得到新的单元数组元素个数和原数组相同。

必须满足R1C1 = R2C2

题目:

查找字符串中给定字符的位置和数目

function test()
    str = input('str = ');
    c = input('c = ');
    index = strfind(str, c);
    disp(index);
    disp(length(index));
  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

输入字符的首字母大写,其他小写

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

文章来源: recclay.blog.csdn.net,作者:ReCclay,版权归原作者所有,如需转载,请联系作者。

原文链接:recclay.blog.csdn.net/article/details/78673303

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。