Matlab中有15种基本数据类型,主要是整型、浮点、逻辑、字符、日期和时间、结构数组、单元格数组以及函数句柄等。

        1、整型:(int8;uint8;int16;uint16;int32;uint32;int64;uint64)通过intmax(class)和intmin(class) 函数返回该类整型的最大值和最小值,例如intmax(‘int8’)=127;

        2、浮点:(single;double)

浮点数:REALMAX('double')和REALMAX('single')分别返回双精度浮点和单精度浮点的最大值,REALMIN('double')和REALMIN ('single')分别返回双精度浮点和单精度浮点的最小值。

3、逻辑:(logical)

Logical:下例是逻辑索引在矩阵操作中的应用,将5*5矩阵中大于0.5的元素设定为0:

       A = rand(5);

       A(A>0.5)=0;

4、字符:(char)

Matlab中的输入字符需使用单引号。字符串存储为字符数组,每个元素占用一个ASCII字符。如日期字符:DateString=’9/16/2001’ 实际上是一个1行9列向量。构成矩阵或向量的行字符串长度必须相同。可以使用char函数构建字符数组,使用strcat函数连接字符。

例如,命令 name = ['abc' ; 'abcd'] 将触发错误警告,因为两个字符串的长度不等,此时可以通过空字符凑齐如:name = ['abc ' ; 'abcd'],更简单的办法是使用char函数:char('abc','abcd'),Matlab自动填充空字符以使长度相等,因此字符串矩阵的列纬总是等于最长字符串的字符数.

例如size(char(‘abc’,’abcd’))返回结果[2,4],即字符串'abc'实际存在的是'abc  ',此时如需提取矩阵中的某一字符元素,需要使用deblank函数移除空格如name =char(‘abc’,’abcd’); deblank(name(1,:))。

此外,Matlab同时提供一种更灵活的单元格数组方法,使用函数cellstr可以将字符串数组转换为单元格数组:

data= char(‘abc’,’abcd’)

       length(data(1,:)) ->4

       cdata=cellstr(data)

       length(cdata{1}) ->3

常用的字符操作函数

deblank(s)移除字符串尾部包含的空字符

(string)将字符串作为命令执行

findstr(s1,s2)搜索字符串

ischar(s)判断是否字符串

isletter(s)判断是否字母

lower(s)转换小写

upper(s)转换大写

strcmp(s1,s2)比较字符串是否相同

strncmp(s1,s2,n)比较字符串中的前n个字符是否相同

strrep(s1,s2,s3)将s1中的字符s2替换为s3

 

5、日期和时间

Matlab提供三种日期格式:日期字符串如’1996-10-02’,日期序列数如729300(0000年1月1日为1)以及日期向量如 1996 10 2 0 0 0,依次为年月日时分秒。

常用的日期操作函数

datestr(d,f)将日期数字转换为字符串

datenum(str,f)将字符串转换为日期数字

datevec(str)日期字符串转换向量

weekday(d)计算星期数

eomday(yr,mth)计算指定月份最后一天

calendar(str)返回日历矩阵

clock当前日期和时间的日期向量

date当前日期字符串

now当前日期和时间的序列数

 

6、结构

结构是包含已命名“数据容器”或字段的数组。结构中的字段可以包含任何数据。

7、构建结构数组

(1)赋值方法

下面的赋值命令产生一个名为patient的结构数组,该数组包含三个字段:

patient.name = 'John Doe';

patient.billing = 127.00;

patient.test = [79 75 73; 180 178 177.5; 220 210 205];

在命令区内输入patient可以查看结构信息:

name: 'John Doe'

billing: 127

test: [3x3 double]

继续赋值可扩展该结构数组:

patient(2).name = 'Ann Lane';

patient(2).billing = 28.50;

patient(2).test = [68 70 68; 118 118 119; 172 170 169];

赋值后结构数组变为[1 2]。

(2)构建结构数组:struct函数 

函数基本形式为:strArray = struct('field1',val1,'field2',val2, ...)

例如:

weather(1) = struct('temp', 72,'rainfall', 0.0);

weather(2) = struct('temp', 71,'rainfall', 0.1);

        weather = repmat(struct('temp', 72, 'rainfall', 0.0), 1, 3);

       weather = struct('temp', {68, 80, 72}, 'rainfall', {0.2, 0.4, 0.0});

(3)访问结构数据

以下都是合法的结构数组访问命令:

mypatients = patient(1:2) 获取子结构数据

mypatients(1) 访问结构数据

patient(2).name 访问结构数据中的特定字段

patient(3).test(2,2) 访问结构数据中的特定字段(该字段为数组)

bills = [patient.billing] 访问多个结构

tests = {patient(1:2).test} 提取结构数据转换成单元格数组

使用结构字段的动态名称

通过structName.(expression_r_r_r)可以赋予结构字段名称并访问数据。例如字段名为expression_r_r_r、结构名为structName,访问其中第7行1至25列数据可以使用命令:structName.(expression_r_r_r)(7,1:25)。

例如,存在一个学生每周成绩数据结构数组,其数据通过以下方式建立:

testscores.wang.week(1:25) = ...

        [95 89 76 82 79 92 94 92 89 81 75 93 ...

         85 84 83 86 85 90 82 82 84 79 96 88 98];

testscores.chen.week(1:25) = ...

                 [87 80 91 84 99 87 93 87 97 87 82 89 ...

                   86 82 90 98 75 79 92 84 90 93 84 78 81];

即结构名为testscores,字段使用每个学生的名称命名,分别为wang和chen,每个学生下面包含名为week的成绩结构数组。

现计算给定结构名称、学生名称和起止周数的平均分数。

在命令窗口中输入 edit avgscore.m,输入以下代码后保存文件:

function avg = avgscore(struct,student, first, last)

       avg = sum(struct.(student).week(first:last))/(last - first + 1);

在命名窗口中输入:avgscore(testscores, 'chen', 7, 22) 计算学生陈从第7周到第22周的平均分数。

(4)添加和删除结构字段

命令[struct](index).(field)可添加或修改字段。如patient(2).ssn = '000-00-0000' 在结构patient中添加一个名为ssn的字段。

删除字段使用rmfield函数,如patient2 = rmfield(patient, 'name') 删除name字段并产生新的结构。

8、单元格数组:(cell)

单元格数组提供了不同类型数据的存储机制,可以储存任意类型和任意纬度的数组。

访问单元格数组的规则和其他数组相同,区别在于需要使用花括号{}访问,例如A{2,5}访问单元格数组A中的第2行第5列单元格。

(1)构建单元格数组:赋值方法

使用花括号标识可直接创建单元格数组,如:

A(1,1) = {[1 4 3; 0 5 8; 7 2 9]};

       A(1,2) = {'abcd'};

       A(2,1) = {3+7i};

        A(2,2) = {-pi:pi/10:pi};

上述命令创建2*2的单元格数组A。继续添加单元格元素直接使用赋值如A(2,3)={5}即可,注意需使用花括号标识。简化的方法是结合使用花括号{单元格数组}和方括号[]创建,如C = {[1 2], [3 4]; [5 6], [7 8]};

(2)构建单元格数组:函数方法

Cell函数。如:

B = cell(2, 3);

B(1,3) = {1:3};

(3)访问数据

通过索引可直接访问单元格数组中的数据元素,例如:

N{1,1} = [1 2; 4 5];

N{1,2} = 'Name';

N{2,1} = 2-4i;

N{2,2} = 7;

c = N{1,2}

d = N{1,1}(2,2)

9、函数句柄

函数句柄是用于间接调用一个函数的Matlab值或数据类型。在调用其它函数时可以传递函数句柄,也可在数据结构中保存函数句柄备用。通过命令形式 fhandle = @functionname 可以创建函数句柄,例如 trigFun=@sin,或匿名函数sqr = @(x) x.^2;。

使用句柄调用函数的形式是 fhandle(arg1, arg2, ..., argn) 或 fhandle()(无参数)。如:

trigFun(1)。例:

function x = plotFHandle(fhandle, data)

plot(data, fhandle(data))

plotFHandle(@sin, -pi:0.01:pi)

数据类型转换如C语言中的强制类型转换相似e.g.:

y=9;

z=double(y);

   Matlab中的图像数据类型转换

MATLAB中读入图像的数据类型是uint8,而在矩阵中使用的数据类型是double因此 I2=im2double(I1) :把图像数组I1转换成double精度类型;如果不转换,在对uint8进行加减时会产生溢出,可能提示的错误为:Function '*' is not defined for values of class 'uint8'。

 

 图像数据类型转换函数

默认情况下,matlab将图像中的数据存储为double型,即64位浮点数;matlab还支持无符号整型(uint8和uint16);uint型的优势在于节省空间,涉及运算时要转换成double型。

    im2double():将图像数组转换成double精度类型

     im2uint8():将图像数组转换成unit8类型

     im2uint16():将图像数组转换成unit16类型

 

//////////////////////////////////////////////

 

%假设你的文件名在c盘叫test.txt

fid=fopen('c:\test.txt');

data=textscan(fid,'%s','delimiter','\n');%把数据按行读入

fclose(fid);%关闭文件

data=data{1,1};

A=textscan(data{1},'%n','delimiter',' ');把第一行按空格为分割赋给A

B=textscan(data{2},'%n','delimiter',' ');同上B

C=textscan(data{3},'%n','delimiter',' ');同上C

A=A{1};

B=B{1};

C=C{1};

把A,B,C由cell转成向量 %这样的话无论这三行分别有多少个数据都可以的。

截取字符串部分:str(2:length(str)-1))

 

textscan 参照:

1.http://www.cnblogs.com/AI-Algorithms/p/3670245.html

2.Matlab >> help textscan

 

源文档 <http://blog.sina.com.cn/s/blog_542a1e4a0100sjpp.html>

 

Escape Characters

 

Character

Description

\n

New line

\t

Horizontal tab

\b

Backspace

\r

Carriage return

\f

Form feed

\\

Backslash

\'' or '' (two single quotes)

Single quotation mark

%%

Percent character

Conversion characters specify the notation of the output.

 

Conversion Specifiers

 

Specifier

Description

%c

Single character

%d

Decimal notation (signed)

%e

Exponential notation (using a lowercase e as in 3.1415e+00)

%E

Exponential notation (using an uppercase E as in 3.1415E+00)

%f

Fixed-point notation

%g

The more compact of %e or %f, as defined in [2]. Insignificant zeros do not print.

%G

Same as %g, but using an uppercase E

%o

Octal notation (unsigned)

%s

String of characters

%u

Decimal notation (unsigned)

%x

Hexadecimal notation (using lowercase letters a-f)

%X

Hexadecimal notation (using uppercase letters A-F)

Other characters can be inserted into the conversion specifier between the % and the conversion character.

 

Other Characters

 

Character

Description

Example

A minus sign (-)

Left-justifies the converted argument in its field.

%-5.2d

A plus sign (+)

Always prints a sign character (+ or -).

%+5.2d

Zero (0)

Pad with zeros rather than spaces.

.2d

Digits (field width)

A digit string specifying the minimum number of digits to be printed.

o

Digits (precision)

A digit string including a period (.) specifying the number of digits to be printed to the right of the decimal point.

 

 

fscanf

format

String enclosed in single quotation marks that describes each type of element (field). Includes one or more of the following specifiers.

Field Type

Specifier

Details

Integer, signed

%d

Base 10

 

%i

Base determined from the values. Defaults to base 10. If initial digits are 0x or 0X, it is base 16. If initial digit is 0, it is base 8.

 

%ld or %li

64-bit values, base 10, 8, or 16

Integer, unsigned

%u

Base 10

 

%o

Base 8 (octal)

 

%x

Base 16 (hexadecimal)

 

%lu, %lo, %lx

64-bit values, base 10, 8, or 16

Floating-point number

%f

Floating-point fields can contain any of the following (not case sensitive): Inf, -Inf, NaN, or -NaN.

 

%e

 

 

%g

 

Character string

%s

Read series of characters, until find white space.

 

%c

Read any single character, including white space.

(To read multiple characters, specify field length.)

 

%[...]

Read only characters in the brackets, until the first nonmatching character or white space.

 

源文档 <http://blog.sina.com.cn/s/blog_65956f180100udlf.html>

Matlab中的数据类型的更多相关文章

  1. $ 一步一步学Matlab(3)——Matlab中的数据类型

    小学时候我们就知道,数学中有自然数.整数.分数.小数等等很多种类型的数.到了中学,我们又发现了其实还有无理数.复数这些有些特殊的数.到了大学学了高等数学之后,我们又知道了其实还存在着无穷大.无穷小这样 ...

  2. Matlab中的基本数据类型介绍

    Matlab中支持的数据类型包括: 逻辑(logical)字符(char)数值(numeric)元胞数组(cell)结构体(structure)表格(table)函数句柄(function handl ...

  3. matlab中读取txt数据文件(txt文本文档)

    matlab中读取txt数据文件(txt文本文档) 根据txt文档不同种类介绍不同的读取数据方法 一.纯数据文件(没有字母和中文,纯数字) 对于这种txt文档,从matalb中读取就简单多了 例如te ...

  4. MATLAB中的结构数组

    MATLAB中的结构数组 结构数组: 结构是包含一组记录的数据类型,而记录则是存储在相应的字段中.结构的字段可以是任意一种MATLAB数据类型的变量或者对象.结构类型的变量也可以是一维的.二维的或多维 ...

  5. Matlab中函数句柄@的作用及介绍

    问:f=@(x)acos(x)表示什么意思?其中@代表什么?答:表示f为函数句柄,@是定义句柄的运算符.f=@(x)acos(x) 相当于建立了一个函数文件:% f.mfunction  y=f(x) ...

  6. matlab中关于函数句柄、feval函数以及inline函数的解析 (转)

    http://blog.sina.com.cn/s/blog_7bff755b010180l3.html MATLAB函数句柄 函数句柄(Function handle)是MATLAB的一种数据类型. ...

  7. 如何使用matlab中的胞元数组

    胞元数组(cell Arry)的基本组分是胞元(cell),每个胞元本身在数组中是平等的,只能以下标区分.胞元可以存放任何类型.任何大小的数组,如任意维数值数组.字符串数组.符号对象等,而且同一个胞元 ...

  8. matlab中图片数据类型转换uint8与double

    matlab中处理图像像素点数据: img1=double(imread('lenna.bmp')); matlab中imshow图片,要先转换成uint8: subplot(1,2,1),imsho ...

  9. matlab的table数据类型初步接触

    由于数据分析,接触到cell的使用,字符串的使用以及ASCII的使用,但是发现在matlab中进行这样的操作相对繁琐,然后知道了table数据类型,是matlab新的数据类型,于2013版开始引入.据 ...

随机推荐

  1. Cornerstone无法上传静态库文件(.a文件)

    在用Cornerstone同步文件时出现一个错误 检查后发现是缺少了一个文件 查询了网上的资料后发现是Cornerstone自动忽略了.a文件,所以上传到svn服务器时.a文件不会跟随工程一起传上去, ...

  2. 苹果Mac隐藏壁纸在哪里?Mac隐藏壁纸查找教程

    Mac隐藏壁纸怎么查找?Mac存不存在隐藏壁纸呢?今天小编就来解决大家的疑问,告诉大家怎么把Mac系统的隐藏壁纸找出来,并且弄能够正常的使用,小编特意写了一个详细的图文教程叫大家如何查找,使用Mac隐 ...

  3. win7 解决git clone 连接被拒绝—hosts文件过期

      我出现问题的原因是自己修改过 C:\Windows\System32\drivers\etc\HOSTS, 把同事的这个文件拷贝过来后,一切恢复了正常   错误在eclipse中表现为:     ...

  4. date\"123456 错误排查

    最近服务器重装,干脆将所有的源代码都重新整理了一下,开始一切正常,后来发现,每次修改一个画面的时候就会报错 跟踪了下发现是datetime.SmartDate等时间类型的数据,在进行序列化的时候改变了 ...

  5. HTTP 协议的头部

    转自:http://network.51cto.com/art/201509/490594.htm HTTP首部字段有四种类型:通用首部字段,请求首部字段,响应首部字段,实体首部字段. 通用首部字段: ...

  6. centos7安装python-pip

    在使用centos7的软件包管理程序yum安装python-pip的时候会报一下错误: No package python-pip available. Error: Nothing to do 说没 ...

  7. hibernate多对多映射关系实现

    Course.hbm.xml: <?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC        &q ...

  8. diff, cmp, patch

    diff 以行为单位比较两个文件之间的差异,经常用来查看同一个文件的新旧版本的差异,通常用在文本文件的比较,可以使用重定向'>'制作补丁文档,通常以.patch结尾 \(diff [-bBi] ...

  9. 笔者的编辑语法:MarkDown

    由于博客园里的文章有很多排版不好,一大堆文字堆在一块会影响到阅读. MarkDowm:百科 Markdown 是一种轻量级标记语言,创始人为约翰·格鲁伯(John Gruber).它允许人们“使用易读 ...

  10. linux sed命令

    一.初识sed 在部署openstack的过程中,会接触到大量的sed命令,比如 # Bind MySQL service to all network interfaces. sed -i 's/1 ...