MATLAB之易经卜卦程序+GUI
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的更多相关文章
- 嵌入式系统WinCE下应用程序GUI界面开发【转】
嵌入式系统WinCE下应用程序GUI界面开发 ByToradex 秦海 本文旨在介绍嵌入式系统在Wince下进行GUI应用程序开发可以选择的不同GUI开发框架(Framework),目前最常用的几种方 ...
- python实现串口通讯小程序(GUI界面)
python实现串口通讯小程序(GUI界面) 使用python实现串口通讯需要使用python的pyserial库来实现,这个库在安装python的时候没有自动进行安装,需要自己进行安装. 1.安装p ...
- Matlab优化存储器读写来改善程序性能
最近用Matlab写程序的时候终于遇到了程序执行效率的问题,于是在Google上面搜索了一篇提高代码性能的文章,简单的概括一下. 文章是通过优化寄存器读写来提高执行速度的,主要体现在三个方面: 在做循 ...
- linux tomcat部署含有matlab画图打包的java web程序
首先说下问题:matlab可以把相关算法代码打包成jar文件共java调用,本例使用的jar文件的作用是画图并保存,然后部署在linux的tomcat中进行发布.这里出现了一个问题,具体如下:linu ...
- C控制台程序 GUI程序
控制台程序对应的工程类型为"Win32控制台程序(Win32 Console Application)",GUI 程序对应的工程类型为"Win32程序(Win32 App ...
- 痞子衡嵌入式:极易上手的可视化wxPython GUI构建工具(wxFormBuilder)
大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是wxPython GUI构建工具wxFormBuilder. 一.手工代码布局GUI界面的烦恼 如果你曾经设计过上位机软件GUI界面,初 ...
- JAVA个人小程序GUI篇-收银(标签、按钮、复选框、下拉标、文本域、表格······)
如果用eclipse需先装载windowsbuild //导入包 import java.awt.BorderLayout; import java.awt.EventQueue; import ja ...
- MATLAB中文论坛帖子整理(GUI)
MATLAB中文论坛帖子整理(GUI) 目 录 1.GUI新手之——教你读懂GUI的M文件... 10 2.GUI程序中改变current directory引起的问题... 15 3.GUI中 ...
- matlab GUI封装exe文件
学习matlab过程中,有时有些程序处理数据时老是看着代码,也会觉得疲倦,那么要试一试matlab的GUI吗?我就是这么使用matlab的GUI制作一个小程序,并且使用matlab封装成了exe文件. ...
随机推荐
- 7.27实习培训日志-Oracle SQL(三)
Oracle SQL(三) 视图 特性 简单视图 复杂视图 关联的表数量 1个 1个或多个 查询中包含函数 否 是 查询中包含分组数据 否 是 允许对视图进行DML操作 是 否 CREATE [OR ...
- 某欧洲电信运营商OSS功能架构
- CSS中position的absolute和relative用法
static: HTML元素的默认定位方式 absolute: 将对象从文档流中拖出,使用left,right,top,bottom等属性进行绝对定位.而其层叠通过z-index属性定义.绝对定位的元 ...
- 关于js模板引擎template的使用记录
引言 有一天在群里有一个人发了这么一个图片 看到这个就会发现2个问题,一个是后期如果html结构改变了,这一大块都要重写.还有一个就是写的时候自己都看不清,很容易出错. 然后还有一个人写的清楚一点,但 ...
- [C++]C,C++中使用可变参数
可变参数即表示参数个数可以变化,可多可少,也表示参数的类型也可以变化,可以是int,double还可以是char*,类,结构体等等.可变参数是实现printf(),sprintf()等函数的关键之处, ...
- PAT 1043【BST与二叉树】
考察: 1.二叉树的建树 2.前序遍历,后序遍历 3.BST的特性 这题的思路: 告诉你数组是先序遍历的,so 根已经知道了(数组首位元素),那么按照BST,建一下树(要两次,另外一次是镜像的): 跑 ...
- elasticsearch 基本介绍
1. Elasticsearch的适用场景: (1)类似百度百科的全文检索,高亮,搜索推荐(2)新闻类的搜索,用户行为日志(点击,浏览,收藏,评论)+社交网络数据(对某某新闻的相关看法),数据分析,给 ...
- 2017-10-17 NOIP模拟赛
Reverse #include<iostream> #include<cstdio> #include<cstring> using namespace std; ...
- 验证控件jQuery Validation Engine调用外部函数验证
在使用jQuery Validation Engine的时候,我们除了使用自带的API之外,还可以自己自定义正则验证.自定义正则验证上一篇已经讲过了,如果想使用自定义函数进行验证怎么办?其实这个控件有 ...
- 源代码实现一个binary例子
一.源代码实现一个binary例子 1.例子描述 (1) 数据描述 输入数据X是二进制的一串序列, 在t时刻,有50%的概率是1,50%的概率是0,比如:X=[1,1,0,0,1,0.....]输出数 ...