MATLAB之易经卜卦程序+GUI

  日月为易,刚柔相推。 是故易有太极,是生两仪,两仪生四象,四象生八卦,八卦定吉凶,吉凶生大业。是故法象莫大乎天地,变通莫大乎四时,悬象著明莫大乎日月。

  本文目的一半是对《易经》卜卦知识的总结,另一半是对利用编程解决实际问题能力的锻炼。至于卜卦,玩玩而已,但是对于《易经》,里面有很多知识值得我们学习。

《易经》知识简介

  本来想重新理一下《易经》知识的,但是尝试一下之后发现三言两语解释不清楚,所以有兴趣的朋友可以读一下以前写的一篇文章

  对于八卦(乾qián、坤kūn、震zhèn、巽xùn、坎kǎn、离lí、艮gèn、兑duì),可以用排列组合知识这样理解:,《易经》中阴阳的概念可对应二进制中的零和一;三位二进制可表示07,分别对应这八个单卦;再由这八个单卦**摩荡**(即排列组合,这工作传说是周文王在狱中完成)而成六十四卦,即六个二进制位表示063,对应六十四卦。相传,图灵发明计算机的二进制概念,灵感就是来源于《易经》。这样也就解释了下面介绍的三钱筮法为什要摇六次。

三钱筮法简介

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

一个背,两个字,称作“单”.画作“ — ”为少阳。

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

三个背,没有字,称作“重”,画作“ 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

MATLAB之易经卜卦程序+GUI的更多相关文章

  1. 嵌入式系统WinCE下应用程序GUI界面开发【转】

    嵌入式系统WinCE下应用程序GUI界面开发 ByToradex 秦海 本文旨在介绍嵌入式系统在Wince下进行GUI应用程序开发可以选择的不同GUI开发框架(Framework),目前最常用的几种方 ...

  2. python实现串口通讯小程序(GUI界面)

    python实现串口通讯小程序(GUI界面) 使用python实现串口通讯需要使用python的pyserial库来实现,这个库在安装python的时候没有自动进行安装,需要自己进行安装. 1.安装p ...

  3. Matlab优化存储器读写来改善程序性能

    最近用Matlab写程序的时候终于遇到了程序执行效率的问题,于是在Google上面搜索了一篇提高代码性能的文章,简单的概括一下. 文章是通过优化寄存器读写来提高执行速度的,主要体现在三个方面: 在做循 ...

  4. linux tomcat部署含有matlab画图打包的java web程序

    首先说下问题:matlab可以把相关算法代码打包成jar文件共java调用,本例使用的jar文件的作用是画图并保存,然后部署在linux的tomcat中进行发布.这里出现了一个问题,具体如下:linu ...

  5. C控制台程序 GUI程序

    控制台程序对应的工程类型为"Win32控制台程序(Win32 Console Application)",GUI 程序对应的工程类型为"Win32程序(Win32 App ...

  6. 痞子衡嵌入式:极易上手的可视化wxPython GUI构建工具(wxFormBuilder)

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是wxPython GUI构建工具wxFormBuilder. 一.手工代码布局GUI界面的烦恼 如果你曾经设计过上位机软件GUI界面,初 ...

  7. JAVA个人小程序GUI篇-收银(标签、按钮、复选框、下拉标、文本域、表格······)

    如果用eclipse需先装载windowsbuild //导入包 import java.awt.BorderLayout; import java.awt.EventQueue; import ja ...

  8. MATLAB中文论坛帖子整理(GUI)

    MATLAB中文论坛帖子整理(GUI) 目   录  1.GUI新手之——教你读懂GUI的M文件... 10 2.GUI程序中改变current directory引起的问题... 15 3.GUI中 ...

  9. matlab GUI封装exe文件

    学习matlab过程中,有时有些程序处理数据时老是看着代码,也会觉得疲倦,那么要试一试matlab的GUI吗?我就是这么使用matlab的GUI制作一个小程序,并且使用matlab封装成了exe文件. ...

随机推荐

  1. Maven Cargo 远程部署到tomcat7x

    pom.xml中加入cargo的Plugin声明: <plugin> <groupId>org.codehaus.cargo</groupId> <artif ...

  2. byte和int转换

    byte b1=1,b2=2,b3,b6; final byte b4=4,b5=6; b6=b4+b5; b3=(b1+b2); System.out.println(b3+b6); b3=b1+b ...

  3. 【转】eclipse中window->preference选项中没有tomcat的解决方法

    eclipse中window->preference选项中没有tomcat的解决方法 2011-09-09 13:46:35|  分类: eclipse|字号 订阅 其实一共有好几种方法,这只是 ...

  4. The project was not built since its build path is incomplete. Cannot find the class file for java.lang.Object

    The project was not built since its build path is incomplete. Cannot find the class file for java.la ...

  5. .NET 中文件嵌套,例如:cshtml文件下面嵌套css和js【机器翻译】

    越来越多的我发现自己在我的一些较大的Web项目中丢失了文件.有很多内容要处理 - HTML视图,几个派生CSS页面,页面级CSS,脚本库,应用程序范围的脚本和页面特定的脚本文件等等.幸运的是,我使用R ...

  6. PHP开源系统学习之fluxbb_1

    最近一直忙于做项目,虽说做了点新东西.感觉自己进步不是很大,总体水平还是跟半年前差不多,想到的东西跟以前差不多,写出来的东西也跟以前差不多.只是现在做的东西多些,比以前敢做了. 近期准备利用点时间,读 ...

  7. 微信小程序小结(3) -- 使用wxParse解析html及多数据循环

    wxParse-微信小程序富文本解析组件:https://github.com/icindy/wxParse 支持Html及markdown转wxml可视化 使用 1.copy下载好的文件夹wxPar ...

  8. struts2学习笔记——常见报错及解决方法汇总(持续更新)

    操作环境:(1)Tomcat 7.0.72.0 (2)OS Name: Windows 7  (3)JVM Version:  1.8.0_25-b18  (4)eclipse Version: Ke ...

  9. ios json转model的简单现实

    在android开发中,可用第三方的转换库如gson等.当然在ios也有一些库如MJExtensiond等.在这里,我简单实现一下. 一.先建一个model并且继承NSObject,代码如下: cla ...

  10. VS2017(Visual Studio2017) 搭建QT5开发环境

    VS创建QT工程并添加到GitHub中 大家好! 欢迎打开并阅读本文,这次咱们说说怎么在VS中创建一个QT项目并且放到GitHub中吧 因为GitHub或者码云Gitee都是一个很好的提供代码托管的地 ...