zoukankan      html  css  js  c++  java
  • MATLAB之易经卜卦程序+GUI

    MATLAB之易经卜卦程序+GUI

      日月为易,刚柔相推。 是故易有太极,是生两仪,两仪生四象,四象生八卦,八卦定吉凶,吉凶生大业。是故法象莫大乎天地,变通莫大乎四时,悬象著明莫大乎日月。
      本文目的一半是对《易经》卜卦知识的总结,另一半是对利用编程解决实际问题能力的锻炼。至于卜卦,玩玩而已,但是对于《易经》,里面有很多知识值得我们学习。

    《易经》知识简介

      本来想重新理一下《易经》知识的,但是尝试一下之后发现三言两语解释不清楚,所以有兴趣的朋友可以读一下以前写的一篇文章
      对于八卦(乾qián、坤kūn、震zhèn、巽xùn、坎kǎn、离lí、艮gèn、兑duì),可以用排列组合知识这样理解:,《易经》中阴阳的概念可对应二进制中的零和一;三位二进制可表示0~7,分别对应这八个单卦;再由这八个单卦摩荡(即排列组合,这工作传说是周文王在狱中完成)而成六十四卦,即六个二进制位表示0~63,对应六十四卦。相传,图灵发明计算机的二进制概念,灵感就是来源于《易经》。这样也就解释了下面介绍的三钱筮法为什要摇六次。

    三钱筮法简介

      三枚铜钱,作为钱筮用具。在焚香致敬祝祷命筮之后,将三枚钱币握于手中,左手在上象征“天”,右手在下象征“地”;随意在手中晃动,再抛掷于地,看三枚钱币的正反情况,确定卦爻的属性。
    一个背,两个字,称作“单”.画作“ — ”为少阳。

    两个背,一个字,称作“拆”,画作“ - - ”为少阴。

    三个背,没有字,称作“重”,画作“ O ”为老阳,为阳爻,是变爻,主过去之事。

    三个字,没有背,称作“交”,画作“ X ”为老阴,为阴爻,是变爻,主未来之事。

      共摇六次,第一次为初爻,画在卦的最下面,依次上升,第六次为第六爻,画在卦的最上边。如遇有X、O,再画出变卦来(由于能力以及对《易经》的了解有限,未能解决变卦的问题(多爻变动如何选择),即本文只考虑给出原始的本卦)。

    卜卦程序设计

    卜卦程序:三枚铜钱,共摇六次,依次输入字面朝上的次数。
    第一步:先计算每次字面朝上次数所对应的阴阳爻(即二进制的零一表示)

    function  [yy] = yao(n)
    % 分析每爻的变化,输入铜钱摇得的字面数
    %输出:yy表示阴阳爻,0为阴,1为阳
    if n<0 || n>3
        error('The num your input is error,please input again ');
    end 
    switch n
        case 0             % 三背为重,老阳,阳爻,是变爻,主过去之事
            yy = 1; 
         case 1            %一字为拆,少阴
            yy = 0;
        case 2             %两字为单,少阳
            yy = 1;
        otherwise       %三字为交,老阴,阴爻,是变爻,主未来之事
            yy = 0;
    end
    end
    

    第二步:由每三次摇得的阴阳爻,分别得出单卦

    function  [gua] = gua(n1,n2,n3)
    % 由三爻得一卦,输入每爻的阴阳
    %输出八卦(后天八卦):乾一、兑二、离三、震四、巽五、坎六、艮七、坤八
    %阳爻时n为1,阴爻时n为0
    gua = 1+(~n3) + (~n2)*2 +(~n1)*4;
    end
    

    第三步:由两个单卦组合,得出本卦

    function name = duan_gua(in,out)
    % 由起卦所得内外卦,输出全卦
    %乾一、兑二、离三、震四、巽五、坎六、艮七、坤八
    if  in<0  ||  in>8 || out<0 || out >8
        disp('The num  is error,please check it ');
        exit;
    end 
    gua_list = {
             '乾为天','天泽履','天火同人','天雷无妄','天风姤','天水讼','天山遁','天地否';
             '泽天夬','兑为泽','泽火革','泽雷随','泽风大过','泽水困','泽山咸','泽地萃';
             '火天大有','火泽睽','离为火','火雷噬嗑','火风鼎','火水未济','火山旅','火地晋';
             '雷天大壮','雷泽归妹','雷火丰','震为雷','雷风恒','雷水解','雷山小过','雷地豫';
             '风天小畜','风泽中孚','风火家人','风雷益','巽为风','风水涣','风山渐','风地观';
             '水天需','水泽节','水火既济','水雷屯','水风井','坎为水','水山蹇','水地比';
             '山天大畜','山泽损','山火贲','山雷颐','山风蛊','山水蒙','艮为山','山地剥';
             '地天泰','地泽临','地火明夷','地雷复','地风升','地水师','地山谦','坤为地';
             };
    name = gua_list(in,out);
    end
    

    主程序:输入六次摇得的铜钱字面朝上数,依次调用各子函数,已经与GUI程序整合

    %三钱筮法MATLAB实现
    %起卦:三枚铜钱,共摇六次,依次输入字面朝上的次数。
    function guaci = SuanGua(number)
    c1 = yao(number(1));
    c2 = yao(number(2));
    c3 = yao(number(3));
    c4 = yao(number(4));
    c5 = yao(number(5));
    c6 = yao(number(6));
    guaci = duan_gua(gua(c1,c2,c3),gua(c4,c5,c6));
    

    GUI界面设计

      图形界面设计使用操作流:在MATLAB的命令窗口中输入guide命令,打开guidequick start窗口,选择create new gui点ok,生成新的fig文件,然后再在窗口中进行各项操作。
    有几点注意事项:
    (1)图片的插入代码段:

    function Changes_OpeningFcn(hObject, eventdata, handles, varargin)
    handles.output = hObject;
     logo1 = importdata('taiji.jpg');              %**图片插入,句柄使用**
     set(handles.logo1, 'CDATA', logo1);
    guidata(hObject, handles);
    

    (2)callback (重要):右击对象,在属性编辑器中修改相关参数(style,tag),再在程序中对应位置修改相关代码段(查看回调函数),保证点击界面时程序执行逻辑顺序正确。
    (3)系统的容错性:仔细考察程序所有可能出现的意外情况,并在程序中解决。比如:当在红色输入框中输入的不是1,2或3时,之前未修改时程序会闪退,修改之后会在红色输出框中显示“error”。

    有兴趣的朋友如果想试试,可以将上面的三个函数和下面的GUI程序保存为m文件,并且将下图中的八卦logo截图保存为taiji.jpg(调整宽度:258,高度244);将这五个文件保存在一个文件夹中,运行GUI程序即可;在红色输入框中依次输入六个数字(1~3,否则报错),点击‘开始断卦’,即可出现如下效果:

    以下代码是在GUI设计窗口操作之后,matlab自动生成的,修改部分代码实现功能。为了保证程序的可读性,并未删除自动生成的大量注释和空行。

    function varargout = Changes(varargin)
    % CHANGES MATLAB code for Changes.fig
    %      CHANGES, by itself, creates a new CHANGES or raises the existing
    %      singleton*.
    %
    %      H = CHANGES returns the handle to a new CHANGES or the handle to
    %      the existing singleton*.
    %
    %      CHANGES('CALLBACK',hObject,eventData,handles,...) calls the local
    %      function named CALLBACK in CHANGES.M with the given input arguments.
    %
    %      CHANGES('Property','Value',...) creates a new CHANGES or raises the
    %      existing singleton*.  Starting from the left, property value pairs are
    %      applied to the GUI before Changes_OpeningFcn gets called.  An
    %      unrecognized property name or invalid value makes property application
    %      stop.  All inputs are passed to Changes_OpeningFcn via varargin.
    %
    %      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
    %      instance to run (singleton)".
    %
    % See also: GUIDE, GUIDATA, GUIHANDLES
    
    % Edit the above text to modify the response to help Changes
    
    % Last Modified by GUIDE v2.5 25-Feb-2017 13:48:13
    
    % Begin initialization code - DO NOT EDIT
    gui_Singleton = 1;
    gui_State = struct('gui_Name',       mfilename, ...
                       'gui_Singleton',  gui_Singleton, ...
                       'gui_OpeningFcn', @Changes_OpeningFcn, ...
                       'gui_OutputFcn',  @Changes_OutputFcn, ...
                       'gui_LayoutFcn',  [] , ...
                       'gui_Callback',   []);
    if nargin && ischar(varargin{1})
        gui_State.gui_Callback = str2func(varargin{1});
    end
    
    if nargout
        [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
    else
        gui_mainfcn(gui_State, varargin{:});
    end
    % End initialization code - DO NOT EDIT
    
    % --- Executes just before Changes is made visible.
    function Changes_OpeningFcn(hObject, eventdata, handles, varargin)
    % This function has no output args, see OutputFcn.
    % hObject    handle to figure
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    % varargin   command line arguments to Changes (see VARARGIN)
    
    % Choose default command line output for Changes
    handles.output = hObject;
     %logo1 = importdata('logo.jpg');
     %set(handles.logo, 'CDATA', logo);
     logo1 = importdata('taiji.jpg');                         %图片插入,使用句柄
     set(handles.logo1, 'CDATA', logo1);
    
    % Update handles structure
    guidata(hObject, handles);
    
    % UIWAIT makes Changes wait for user response (see UIRESUME)
    % uiwait(handles.figure1);
    
    % --- Outputs from this function are returned to the command line.
    function varargout = Changes_OutputFcn(hObject, eventdata, handles) 
    % varargout  cell array for returning output args (see VARARGOUT);
    % hObject    handle to figure
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    
    % Get default command line output from handles structure
    varargout{1} = handles.output;
    
    % --- Executes when figure1 is resized.
    function figure1_SizeChangedFcn(hObject, eventdata, handles)
    % hObject    handle to figure1 (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    
    function edit1_Callback(hObject, eventdata, handles)
    % hObject    handle to edit1 (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    
    % Hints: get(hObject,'String') returns contents of edit1 as text
    %        str2double(get(hObject,'String')) returns contents of edit1 as a double
    
    % --- Executes during object creation, after setting all properties.
    function edit1_CreateFcn(hObject, eventdata, handles)
    % hObject    handle to edit1 (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    empty - handles not created until after all CreateFcns called
    
    % Hint: edit controls usually have a white background on Windows.
    %       See ISPC and COMPUTER.
    if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
        set(hObject,'BackgroundColor','white');
    end
    
    function edit2_Callback(hObject, eventdata, handles)
    % hObject    handle to edit2 (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    
    % Hints: get(hObject,'String') returns contents of edit2 as text
    %        str2double(get(hObject,'String')) returns contents of edit2 as a double
    
    % --- Executes during object creation, after setting all properties.
    function edit2_CreateFcn(hObject, eventdata, handles)
    % hObject    handle to edit2 (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    empty - handles not created until after all CreateFcns called
    
    % Hint: edit controls usually have a white background on Windows.
    %       See ISPC and COMPUTER.
    if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
        set(hObject,'BackgroundColor','white');
    end
    
    function edit3_Callback(hObject, eventdata, handles)
    % hObject    handle to edit3 (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    
    % Hints: get(hObject,'String') returns contents of edit3 as text
    %        str2double(get(hObject,'String')) returns contents of edit3 as a double
    
    % --- Executes during object creation, after setting all properties.
    function edit3_CreateFcn(hObject, eventdata, handles)
    % hObject    handle to edit3 (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    empty - handles not created until after all CreateFcns called
    
    % Hint: edit controls usually have a white background on Windows.
    %       See ISPC and COMPUTER.
    if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
        set(hObject,'BackgroundColor','white');
    end
    
    function edit4_Callback(hObject, eventdata, handles)
    % hObject    handle to edit4 (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    
    % Hints: get(hObject,'String') returns contents of edit4 as text
    %        str2double(get(hObject,'String')) returns contents of edit4 as a double
    
    
    % --- Executes during object creation, after setting all properties.
    function edit4_CreateFcn(hObject, eventdata, handles)
    % hObject    handle to edit4 (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    empty - handles not created until after all CreateFcns called
    
    % Hint: edit controls usually have a white background on Windows.
    %       See ISPC and COMPUTER.
    if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
        set(hObject,'BackgroundColor','white');
    end
    
    function edit5_Callback(hObject, eventdata, handles)
    % hObject    handle to edit5 (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    
    % Hints: get(hObject,'String') returns contents of edit5 as text
    %        str2double(get(hObject,'String')) returns contents of edit5 as a double
    
    
    % --- Executes during object creation, after setting all properties.
    function edit5_CreateFcn(hObject, eventdata, handles)
    % hObject    handle to edit5 (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    empty - handles not created until after all CreateFcns called
    
    % Hint: edit controls usually have a white background on Windows.
    %       See ISPC and COMPUTER.
    if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
        set(hObject,'BackgroundColor','white');
    end
    
    function edit6_Callback(hObject, eventdata, handles)
    % hObject    handle to edit6 (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    
    % Hints: get(hObject,'String') returns contents of edit6 as text
    %        str2double(get(hObject,'String')) returns contents of edit6 as a double
    
    % --- Executes during object creation, after setting all properties.
    function edit6_CreateFcn(hObject, eventdata, handles)
    % hObject    handle to edit6 (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    empty - handles not created until after all CreateFcns called
    
    % Hint: edit controls usually have a white background on Windows.
    %       See ISPC and COMPUTER.
    if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
        set(hObject,'BackgroundColor','white');
    end
    
    function edit7_Callback(hObject, eventdata, handles)
    % hObject    handle to edit7 (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    
    % Hints: get(hObject,'String') returns contents of edit7 as text
    %        str2double(get(hObject,'String')) returns contents of edit7 as a double
    
    % --- Executes during object creation, after setting all properties.
    function edit7_CreateFcn(hObject, eventdata, handles)
    % hObject    handle to edit7 (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    empty - handles not created until after all CreateFcns called
    
    % Hint: edit controls usually have a white background on Windows.
    %       See ISPC and COMPUTER.
    if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
        set(hObject,'BackgroundColor','white');
    end
    
    % --- Executes on button press in pushbutton1.
    function pushbutton1_Callback(hObject, eventdata, handles)
    % hObject    handle to pushbutton1 (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    n1 = get(handles.edit1,'string');          %**开始断卦对应的回调函数**  
    n1 = str2num(n1);
    n2 = get(handles.edit2,'string');                      %输入 
    n2 = str2num(n2);
    n3 = get(handles.edit3,'string');
    n3 = str2num(n3);
    n4 = get(handles.edit4,'string');
    n4 = str2num(n4);
    n5 = get(handles.edit5,'string');
    n5 = str2num(n5);
    n6 = get(handles.edit6,'string');
    n6 = str2num(n6);
    number = [n1,n2,n3,n4,n5,n6];
    
    if length(number)~=6
        set(handles.edit7,'string','error');
    else
        guaci = SuanGua(number);                          %算卦程序处理
        set(handles.edit7,'string',guaci);                %输出
    end
    
    % --- Executes on button press in pushbutton2.
    function pushbutton2_Callback(hObject, eventdata, handles)
    % hObject    handle to pushbutton2 (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    close(Changes);
    
    % --- If Enable == 'on', executes on mouse press in 5 pixel border.
    % --- Otherwise, executes on mouse press in 5 pixel border or over text5.
    function text5_ButtonDownFcn(hObject, eventdata, handles)
    % hObject    handle to text5 (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    
    
    % --- Executes on button press in logo.
    function logo_Callback(hObject, eventdata, handles)
    % hObject    handle to logo (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    
    % --- Executes on button press in logo1.
    function logo1_Callback(hObject, eventdata, handles)
    % hObject    handle to logo1 (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    
    function edit11_Callback(hObject, eventdata, handles)
    % hObject    handle to edit11 (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
    
    % Hints: get(hObject,'String') returns contents of edit11 as text
    %        str2double(get(hObject,'String')) returns contents of edit11 as a double
    
    % --- Executes during object creation, after setting all properties.
    function edit11_CreateFcn(hObject, eventdata, handles)
    % hObject    handle to edit11 (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    empty - handles not created until after all CreateFcns called
    
    % Hint: edit controls usually have a white background on Windows.
    %       See ISPC and COMPUTER.
    if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
        set(hObject,'BackgroundColor','white');
    end
    
  • 相关阅读:
    【Shell】Shell编程之while循环命令
    【Shell】Shell编程之grep命令
    【Shell】Shell编程之awk命令
    【Shell】Shell编程之sed命令
    【MySQL】MySQL之示例数据库Sakila下载及安装
    关于JAVA项目报表选型过程
    关于HSQLDB访问已有数据库文件的操作说明
    为什么要用 C# 来作为您的首选编程语言
    ThinkPHP_5对数据库的CURL操作
    使用ECharts画K线图
  • 原文地址:https://www.cnblogs.com/HZL2017/p/6659998.html
Copyright © 2011-2022 走看看