本文将介绍一种非常重要的概念,枚举量,以及它在simulink状态机中的使用,并且给出模型,方便大家学习。
 枚举量:实际上是用一个名字表示了一个变量,能够比较方便的表示标志信息
A.简单举例:
1)在当前目录(或者matlab搜索路径中)创建一个类文件BulbStatus .m,粘贴如下枚举量定义的类;
 2)这里的classdef后面的类名要和文件名一致,<号后面是类型;
classdef BulbStatus < Simulink.IntEnumType
    enumeration
        On(0)
        Off(1)
    end
    methods (Static)
        function retVal = getDefaultValue()
            % 定义默认值
            retVal  = BulbStatus.On;
        end        
        function retVal = getDescription()
            % 描述信息
            retVal = '灯泡的状态';
        end        
        function retVal = getHeaderFile()
            % 头文件
            retVal = 'BulbStatusDef.h';
        end        
        function retVal = getDataScope()
            % DataScope
            retVal = 'Exported';
        end        
        function retVal = addClassNameToEnumNames()
            % 将枚举量名称作为前缀表现在成员变量名字里
            retVal = true;
        end
    end
end
- 使用方法,添加m文件到系统环境环境:
 m文件创建的枚举量,直接在
 BulbStatus.On 就可以,它就表示的是0
B,项目实践案例,以及注意事项
 首先新建一个m文件,名称是chart_statu.m,然后填写下面的内容,这个m文件创建枚举量,m文件是不需要运行的,只需要加入到系统环境里去就可以了。
classdef(Enumeration) chart_statu < Simulink.IntEnumType
    enumeration
        Stop(0)
        Move(1)
        ACTIVE(2)
    end
    methods (Static = true)
        function descr = getDescription()
            descr = 'Enumeration of fast off state';
        end
        function hdrFile = getHeaderFile()
            hdrFile = '';
        end
		  
        function retVal = getDataScope()
            retVal = 'Exported';
        end
        function retVal = addClassNameToEnumNames()
            retVal = true;
        end
        function defaultValue = getDefaultValue()
            defaultValue = chart_statu.Stop;
        end
    end
end


 

然后运行就可以看到结果,非常好用
 
C:除了在状态机中使用,在普通模块中使用也是可以的
举例:Simulink配置:枚举量配置



















