├── .gitattributes ├── README.MD ├── assets ├── CheatSheet │ ├── MatlabCommands.pdf │ ├── matlab-cheatsheet.pdf │ └── matlab_quickref.pdf ├── Effective_Matlab │ ├── Good Matlab Programming Practices_1.pdf │ ├── Good Matlab Programming Practices_2.pdf │ ├── MATLAB Coding Checklist.pdf │ ├── MATLAB Coding Checklist.zip │ ├── MATLAB之父-编程实践-中译本_200.pdf │ ├── MATLAB高效编程技巧与应用_25个案例分析%28完整版%29.pdf │ ├── Matlab Style Guidelines Cheat Sheet.zip │ ├── MatlabStyle1p5.pdf │ ├── MatlabStyle1p5.zip │ ├── MatlabStyle2 book.pdf │ ├── The Elements of MATLAB Style (Richard K. Johnson).pdf │ ├── WritingFastMatlabCode.pdf │ └── matlab-guidelines.pdf ├── Matlab.png ├── Matlab编程(第二版).pdf ├── Quote.pptx ├── getstart.pdf ├── matlab自上而下编程方法.png ├── matlab调试.png ├── matlab调试2.png ├── 《Matlab编程》源码 │ ├── chap1 │ │ ├── calc_area.m │ │ └── sin_x.m │ ├── chap10 │ │ ├── HistGUI1.m │ │ ├── MyFirstGUI │ │ │ ├── MyFirstGUI.fig │ │ │ └── MyFirstGUI.m │ │ ├── MyFirstGUI1 │ │ │ ├── MyFirstGUI.fig │ │ │ └── MyFirstGUI.m │ │ ├── fig10-2.ppt │ │ ├── fig10-7.ppt │ │ ├── fig10-8.ppt │ │ ├── fig10_1.fig │ │ ├── fig10_1.m │ │ ├── histGUI.fig │ │ ├── histGUI.m │ │ ├── histGUI │ │ │ ├── HistGUI1.m │ │ │ ├── histGUI.fig │ │ │ ├── histGUI.m │ │ │ ├── histGUI1.fig │ │ │ └── test_hist.dat │ │ ├── histGUI1.fig │ │ ├── input1.dat │ │ ├── junk │ │ │ ├── MyFirstGUI.fig │ │ │ └── MyFirstGUI.m │ │ ├── menu_test.fig │ │ ├── menu_test.m │ │ ├── panels.fig │ │ ├── panels.m │ │ ├── plot_function.fig │ │ ├── plot_function.m │ │ ├── plot_line.fig │ │ ├── plot_line.m │ │ ├── temp_conversion │ │ │ ├── temp_conversion.fig │ │ │ ├── temp_conversion.m │ │ │ ├── to_c.m │ │ │ └── to_f.m │ │ ├── test_button_group.fig │ │ ├── test_button_group.m │ │ ├── test_button_group1.m │ │ ├── test_checkbox.fig │ │ ├── test_checkbox.m │ │ ├── test_edit.fig │ │ ├── test_edit.m │ │ ├── test_edit2.fig │ │ ├── test_edit2.m │ │ ├── test_listbox_1.fig │ │ ├── test_listbox_1.m │ │ ├── test_menu1.fig │ │ ├── test_menu1.m │ │ ├── test_popup_menu.fig │ │ ├── test_popup_menu.m │ │ ├── test_radio_button.fig │ │ ├── test_radio_button.m │ │ ├── test_slider.fig │ │ ├── test_slider.m │ │ ├── test_textbox.fig │ │ ├── test_textbox.m │ │ ├── test_toggle.fig │ │ └── test_toggle.m │ ├── chap2 │ │ ├── c14_date.m │ │ ├── calc_area.m │ │ ├── calc_power.m │ │ ├── fig2_12.m │ │ ├── fprintf_example.m │ │ ├── quiz_area.m │ │ ├── sin_x.m │ │ ├── temp_conversion.m │ │ ├── test.m │ │ └── xxx.dat │ ├── chap3 │ │ ├── calc_roots.m │ │ ├── fig3-13.fig │ │ ├── fig3-15.fig │ │ ├── funxy.m │ │ ├── ideal_gas.m │ │ ├── junk.m │ │ ├── microphone.m │ │ └── plot_filter.m │ ├── chap4 │ │ ├── ball.m │ │ ├── ball_2 │ │ │ └── ball.m │ │ ├── doy.m │ │ ├── logical1.m │ │ ├── lsqfit.m │ │ ├── nested_1.m │ │ ├── sqr.m │ │ ├── stats_1.m │ │ ├── stats_2.m │ │ ├── stats_3.m │ │ ├── test_break.m │ │ ├── test_continue.m │ │ └── timings.m │ ├── chap5 │ │ ├── dist2.m │ │ ├── fact.m │ │ ├── fig5_12c.m │ │ ├── mystats.m │ │ ├── polar2rect.m │ │ ├── polar_value.m │ │ ├── private │ │ │ ├── sin.m │ │ │ └── sin1.m │ │ ├── quickplot.m │ │ ├── random0.m │ │ ├── rect2polar.m │ │ ├── runstats.m │ │ ├── sample.m │ │ ├── seed.m │ │ ├── ssort.m │ │ ├── test_dist2.m │ │ ├── test_nested_1.m │ │ ├── test_nested_2.m │ │ ├── test_runstats.m │ │ ├── test_sample.m │ │ └── test_ssort.m │ ├── chap6 │ │ ├── bar_plot.m │ │ ├── barh_plot.m │ │ ├── c_strcmp.m │ │ ├── calc_roots2.m │ │ ├── compass_plot.m │ │ ├── fig6_12a_test_mesh.m │ │ ├── fig6_12b_test_surf.m │ │ ├── fig6_12c_test_contour.m │ │ ├── pie_plot.m │ │ ├── stair_plot.m │ │ ├── stem_plot.m │ │ ├── test_contour2.m │ │ ├── test_ezplot.m │ │ ├── test_fplot.m │ │ ├── test_hist.m │ │ ├── test_plot.m │ │ └── test_plot3.m │ ├── chap7 │ │ ├── calc_average.m │ │ ├── calc_decay.m │ │ ├── count_calls.m │ │ ├── decay1.m │ │ ├── fact1.m │ │ ├── fun1.m │ │ ├── gensparse.m │ │ ├── my_func.m │ │ ├── ode45_test1.m │ │ ├── plotfunc.m │ │ ├── plotline.m │ │ ├── simul.m │ │ ├── sparse1.mat │ │ ├── student.mat │ │ ├── temp.m │ │ ├── test1.m │ │ ├── test2.m │ │ └── test_preallocate.m │ ├── chap8 │ │ ├── binary_io.m │ │ ├── compare.m │ │ ├── formatted.dat │ │ ├── input1 │ │ ├── input2 │ │ ├── int.dat │ │ ├── lsqfit.m │ │ ├── lsqfit_plot.m │ │ ├── output.m │ │ ├── sin_cos_table.m │ │ ├── table.m │ │ ├── test_input.dat │ │ ├── test_input1.dat │ │ ├── test_matfile.mat │ │ ├── testfile │ │ ├── text grades.xls │ │ ├── unformatted.dat │ │ └── workspace.mat │ ├── chap9 │ │ ├── fig10_1.fig │ │ ├── fig10_1.m │ │ ├── plotsinc.m │ │ ├── position_object.m │ │ ├── select_object.m │ │ ├── test1.fig │ │ ├── test1.m │ │ └── test1_export.m │ └── temp_conversion.m ├── 内置函数 ├── 图形对象 ├── 图形对象层次结构 └── 捷径表达式 └── docs ├── Effective_Matlab.md ├── Matlab最佳实践.md ├── Matlab的高级功能及进阶使用.md ├── Matlab科学计算.md ├── Matlab绘图技巧及经验总结.md ├── Matlab编程珠玑.md ├── Matlab编程规范.md └── matlab技巧_快捷键_注意事项.md /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /README.MD: -------------------------------------------------------------------------------- 1 |
2 | 3 |
4 | 5 |

6 |
7 | 8 | ## 目录 9 | ## :bread: 基础 10 | 11 | 1. #### Matlab 基础教程 12 | - [MATLAB 快速入门(官网)](https://ww2.mathworks.cn/help/matlab/getting-started-with-matlab.html) 📓[PDF英文版本](assets/getstart.pdf) 13 | - [语言基础知识——语法、运算符、数据类型、数组索引及处理(官网)](https://ww2.mathworks.cn/help/matlab/language-fundamentals.html) 14 | - [图形——二维和三维绘图、图像、动画、可视化(官网)](https://ww2.mathworks.cn/help/matlab/graphics.html) 15 | - [数据导入和分析——导入和导出、预处理、可视化探查(官网)](https://ww2.mathworks.cn/help/matlab/data-import-and-analysis.html) 16 | ```language 17 | 注: 18 | Matlab 基础部分只需要利用好官网的教程,把上面 4 部分内容过一遍,平时多使用语法帮助命令 help,doc,demo。 19 | ``` 20 | 21 | 2. #### Matlab 编程 22 | - [脚本和函数编程——程序文件、控制流、编辑、调试(官网)](https://ww2.mathworks.cn/help/matlab/programming-and-data-types.html) 23 | - [《Matlab 编程》 第二版 Stephen J. Chapman 著 邢树军 郑碧波译 ](/assets/Matlab编程(第二版).pdf) 🗃[源代码](assets/《Matlab编程》源码) 24 | 25 | 26 | ``` 27 | 注: 28 | Matlab 脚本和函数编程部分除了官方教程外,推荐一本《Matlab 编程》第二版,里面有大量的指导和练习,有助于养成良好的代码习惯。 29 | ``` 30 | ## 💻 进阶 31 | 1. #### Matlab 高级编程 32 | - [高级软件开发——面向对象的编程;代码性能;单元测试;与 Java® 和 Web 服务、C/C++、.NET 和其他语言的外部接口(官网)](https://ww2.mathworks.cn/help/matlab/software-development.html) 33 | 2. #### Matlab GUI 编程 34 | - [App 构建——使用 App 设计工具、GUIDE 或编程工作流进行 App 开发(官网)](https://ww2.mathworks.cn/help/matlab/gui-development.html) 35 | 36 | 37 | 38 | 39 | ## 📏 应用 40 | 1. #### 科学计算 41 | 1. 《Matlab 在科学计算中的应用》国科大 申立勇 [PPT课件及作业](《Matlab在科学计算中的应用》PPT课件及作业) 42 | 43 | ## 🍒 资源 44 | - [MATLAB® Central](https://www.mathworks.com/matlabcentral/index.html) 45 | - [MATLAB Answers 问答网站](https://www.mathworks.com/matlabcentral/answers/index/?s_tid=gn_mlc_an) 46 | - [File Exchange 代码文件分享网站](https://www.mathworks.com/matlabcentral/fileexchange/?s_tid=gn_mlc_fx) 47 | - [Cody 题库](https://www.mathworks.com/matlabcentral/cody/?s_tid=gn_mlc_cody) 48 | 49 | - [Matlab 中文论坛](https://www.ilovematlab.cn/) 50 | - [MATLAB–Python–Julia cheatsheet](https://cheatsheets.quantecon.org/) 51 | 52 | 53 | ## ✍笔记整理 54 | - [《Effective Matlab 编程笔记》](docs/Effective_Matlab.md) 55 | 56 | - [《Matlab 科学计算复习笔记》](docs/Matlab科学计算.md) 57 | 58 | 59 | ### :memo: 后记 60 | ### License 61 | 知识共享许可协议
本作品采用知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议进行许可。 -------------------------------------------------------------------------------- /assets/CheatSheet/MatlabCommands.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/101Hub/Matlab101/07273f68f1147a110443aeb121fa10962234f298/assets/CheatSheet/MatlabCommands.pdf -------------------------------------------------------------------------------- /assets/CheatSheet/matlab-cheatsheet.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/101Hub/Matlab101/07273f68f1147a110443aeb121fa10962234f298/assets/CheatSheet/matlab-cheatsheet.pdf -------------------------------------------------------------------------------- /assets/CheatSheet/matlab_quickref.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/101Hub/Matlab101/07273f68f1147a110443aeb121fa10962234f298/assets/CheatSheet/matlab_quickref.pdf -------------------------------------------------------------------------------- /assets/Effective_Matlab/Good Matlab Programming Practices_1.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/101Hub/Matlab101/07273f68f1147a110443aeb121fa10962234f298/assets/Effective_Matlab/Good Matlab Programming Practices_1.pdf -------------------------------------------------------------------------------- /assets/Effective_Matlab/Good Matlab Programming Practices_2.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/101Hub/Matlab101/07273f68f1147a110443aeb121fa10962234f298/assets/Effective_Matlab/Good Matlab Programming Practices_2.pdf -------------------------------------------------------------------------------- /assets/Effective_Matlab/MATLAB Coding Checklist.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/101Hub/Matlab101/07273f68f1147a110443aeb121fa10962234f298/assets/Effective_Matlab/MATLAB Coding Checklist.pdf -------------------------------------------------------------------------------- /assets/Effective_Matlab/MATLAB Coding Checklist.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/101Hub/Matlab101/07273f68f1147a110443aeb121fa10962234f298/assets/Effective_Matlab/MATLAB Coding Checklist.zip -------------------------------------------------------------------------------- /assets/Effective_Matlab/MATLAB之父-编程实践-中译本_200.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/101Hub/Matlab101/07273f68f1147a110443aeb121fa10962234f298/assets/Effective_Matlab/MATLAB之父-编程实践-中译本_200.pdf -------------------------------------------------------------------------------- /assets/Effective_Matlab/MATLAB高效编程技巧与应用_25个案例分析%28完整版%29.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/101Hub/Matlab101/07273f68f1147a110443aeb121fa10962234f298/assets/Effective_Matlab/MATLAB高效编程技巧与应用_25个案例分析%28完整版%29.pdf -------------------------------------------------------------------------------- /assets/Effective_Matlab/Matlab Style Guidelines Cheat Sheet.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/101Hub/Matlab101/07273f68f1147a110443aeb121fa10962234f298/assets/Effective_Matlab/Matlab Style Guidelines Cheat Sheet.zip -------------------------------------------------------------------------------- /assets/Effective_Matlab/MatlabStyle1p5.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/101Hub/Matlab101/07273f68f1147a110443aeb121fa10962234f298/assets/Effective_Matlab/MatlabStyle1p5.pdf -------------------------------------------------------------------------------- /assets/Effective_Matlab/MatlabStyle1p5.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/101Hub/Matlab101/07273f68f1147a110443aeb121fa10962234f298/assets/Effective_Matlab/MatlabStyle1p5.zip -------------------------------------------------------------------------------- /assets/Effective_Matlab/MatlabStyle2 book.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/101Hub/Matlab101/07273f68f1147a110443aeb121fa10962234f298/assets/Effective_Matlab/MatlabStyle2 book.pdf -------------------------------------------------------------------------------- /assets/Effective_Matlab/The Elements of MATLAB Style (Richard K. Johnson).pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/101Hub/Matlab101/07273f68f1147a110443aeb121fa10962234f298/assets/Effective_Matlab/The Elements of MATLAB Style (Richard K. Johnson).pdf -------------------------------------------------------------------------------- /assets/Effective_Matlab/WritingFastMatlabCode.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/101Hub/Matlab101/07273f68f1147a110443aeb121fa10962234f298/assets/Effective_Matlab/WritingFastMatlabCode.pdf -------------------------------------------------------------------------------- /assets/Effective_Matlab/matlab-guidelines.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/101Hub/Matlab101/07273f68f1147a110443aeb121fa10962234f298/assets/Effective_Matlab/matlab-guidelines.pdf -------------------------------------------------------------------------------- /assets/Matlab.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/101Hub/Matlab101/07273f68f1147a110443aeb121fa10962234f298/assets/Matlab.png -------------------------------------------------------------------------------- /assets/Matlab编程(第二版).pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/101Hub/Matlab101/07273f68f1147a110443aeb121fa10962234f298/assets/Matlab编程(第二版).pdf -------------------------------------------------------------------------------- /assets/Quote.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/101Hub/Matlab101/07273f68f1147a110443aeb121fa10962234f298/assets/Quote.pptx -------------------------------------------------------------------------------- /assets/getstart.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/101Hub/Matlab101/07273f68f1147a110443aeb121fa10962234f298/assets/getstart.pdf -------------------------------------------------------------------------------- /assets/matlab自上而下编程方法.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/101Hub/Matlab101/07273f68f1147a110443aeb121fa10962234f298/assets/matlab自上而下编程方法.png -------------------------------------------------------------------------------- /assets/matlab调试.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/101Hub/Matlab101/07273f68f1147a110443aeb121fa10962234f298/assets/matlab调试.png -------------------------------------------------------------------------------- /assets/matlab调试2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/101Hub/Matlab101/07273f68f1147a110443aeb121fa10962234f298/assets/matlab调试2.png -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap1/calc_area.m: -------------------------------------------------------------------------------- 1 | % This m-file calculates the area of a circle, 2 | % and displays the result. 3 | radius = 2.5; 4 | area = pi * 2.5^2; 5 | string = ['The area of the circle is ' num2str(area)]; 6 | disp(string); 7 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap1/sin_x.m: -------------------------------------------------------------------------------- 1 | % sin_x.m: This m-file calculates and plots the 2 | % function sin(x) for 0 <= x <= 6. 3 | x = 0:0.1:6 4 | y = sin(x) 5 | plot(x,y) 6 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap10/HistGUI1.m: -------------------------------------------------------------------------------- 1 | function varargout = histGUI1(varargin) 2 | % HISTGUI Application M-file for histGUI.fig 3 | % FIG = HISTGUI launch histGUI GUI. 4 | % HISTGUI('callback_name', ...) invoke the named callback. 5 | 6 | % Last Modified by GUIDE v2.0 29-Jul-2001 11:18:39 7 | 8 | if nargin == 0 % LAUNCH GUI 9 | 10 | fig = openfig(mfilename,'reuse'); 11 | 12 | % Generate a structure of handles to pass to callbacks. 13 | handles = guihandles(fig); 14 | 15 | % Add the number of bins to this structure. 16 | handles.nbins = str2num(get(handles.NBins,'String')); 17 | 18 | % Save handles structure 19 | guidata(fig, handles); 20 | 21 | if nargout > 0 22 | varargout{1} = fig; 23 | end 24 | 25 | elseif ischar(varargin{1}) % INVOKE NAMED SUBFUNCTION OR CALLBACK 26 | 27 | try 28 | [varargout{1:nargout}] = feval(varargin{:}); % FEVAL switchyard 29 | catch 30 | disp(lasterr); 31 | end 32 | 33 | end 34 | 35 | 36 | %| ABOUT CALLBACKS: 37 | %| GUIDE automatically appends subfunction prototypes to this file, and 38 | %| sets objects' callback properties to call them through the FEVAL 39 | %| switchyard above. This comment describes that mechanism. 40 | %| 41 | %| Each callback subfunction declaration has the following form: 42 | %| (H, EVENTDATA, HANDLES, VARARGIN) 43 | %| 44 | %| The subfunction name is composed using the object's Tag and the 45 | %| callback type separated by '_', e.g. 'slider2_Callback', 46 | %| 'figure1_CloseRequestFcn', 'axis1_ButtondownFcn'. 47 | %| 48 | %| H is the callback object's handle (obtained using GCBO). 49 | %| 50 | %| EVENTDATA is empty, but reserved for future use. 51 | %| 52 | %| HANDLES is a structure containing handles of components in GUI using 53 | %| tags as fieldnames, e.g. handles.figure1, handles.slider2. This 54 | %| structure is created at GUI startup using GUIHANDLES and stored in 55 | %| the figure's application data using GUIDATA. A copy of the structure 56 | %| is passed to each callback. You can store additional information in 57 | %| this structure at GUI startup, and you can change the structure 58 | %| during callbacks. Call guidata(h, handles) after changing your 59 | %| copy to replace the stored original so that subsequent callbacks see 60 | %| the updates. Type "help guihandles" and "help guidata" for more 61 | %| information. 62 | %| 63 | %| VARARGIN contains any extra arguments you have passed to the 64 | %| callback. Specify the extra arguments by editing the callback 65 | %| property in the inspector. By default, GUIDE sets the property to: 66 | %| ('', gcbo, [], guidata(gcbo)) 67 | %| Add any extra arguments after the last argument, before the final 68 | %| closing parenthesis. 69 | 70 | 71 | % -------------------------------------------------------------------- 72 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 73 | % Callback to open and read a file. 74 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 75 | function varargout = FileOpen1_Callback(h, eventdata, handles, varargin) 76 | 77 | % Get file name 78 | [filename,path] = uigetfile('*.dat','Load Data File'); 79 | if filename ~= 0 80 | 81 | % Read data 82 | x = textread([path filename],'%f'); 83 | 84 | % Save in handles structure 85 | handles.x = x; 86 | guidata(gcbf, handles); 87 | 88 | % Create histogram 89 | hist(handles.x,handles.nbins); 90 | 91 | % Set axis labels 92 | xlabel('\bfValue'); 93 | ylabel('\bfCount'); 94 | 95 | % Calculate statistics 96 | ave = mean(x); 97 | med = median(x); 98 | sd = std(x); 99 | n = length(x); 100 | 101 | % Update fields 102 | set (handles.MeanData,'String',sprintf('%7.2f',ave)); 103 | set (handles.MedianData,'String',sprintf('%7.2f',med)); 104 | set (handles.StdDevData,'String',sprintf('%7.2f',sd)); 105 | set (handles.TitleString,'String',['Histogram (N = ' int2str(n) ')']); 106 | 107 | end 108 | 109 | 110 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 111 | % Callback to exit. 112 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 113 | function varargout = FileExit1_Callback(h, eventdata, handles, varargin) 114 | close(gcbf); 115 | 116 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 117 | % Callback to set number of bins. 118 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 119 | function varargout = NBins_Callback(h, eventdata, handles, varargin) 120 | 121 | % Get number of bins, round to integer, and update field 122 | nbins = str2num(get(gcbo,'String')); 123 | nbins = round(nbins); 124 | if nbins < 1 125 | nbins = 1; 126 | end 127 | set (handles.NBins,'String',int2str(nbins)); 128 | 129 | % Save in handles structure 130 | handles.nbins = nbins; 131 | guidata(gcbf, handles); 132 | 133 | % Re-display data, if available 134 | if handles.nbins > 0 & ~isempty(handles.x) 135 | 136 | % Create histogram 137 | hist(handles.x,handles.nbins); 138 | 139 | end 140 | 141 | 142 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap10/MyFirstGUI/MyFirstGUI.fig: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/101Hub/Matlab101/07273f68f1147a110443aeb121fa10962234f298/assets/《Matlab编程》源码/chap10/MyFirstGUI/MyFirstGUI.fig -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap10/MyFirstGUI/MyFirstGUI.m: -------------------------------------------------------------------------------- 1 | function varargout = MyFirstGUI(varargin) 2 | % MYFIRSTGUI M-file for MyFirstGUI.fig 3 | % MYFIRSTGUI, by itself, creates a new MYFIRSTGUI or raises the existing 4 | % singleton*. 5 | % 6 | % H = MYFIRSTGUI returns the handle to a new MYFIRSTGUI or the handle to 7 | % the existing singleton*. 8 | % 9 | % MYFIRSTGUI('CALLBACK',hObject,eventData,handles,...) calls the local 10 | % function named CALLBACK in MYFIRSTGUI.M with the given input arguments. 11 | % 12 | % MYFIRSTGUI('Property','Value',...) creates a new MYFIRSTGUI or raises the 13 | % existing singleton*. Starting from the left, property value pairs are 14 | % applied to the GUI before MyFirstGUI_OpeningFunction gets called. An 15 | % unrecognized property name or invalid value makes property application 16 | % stop. All inputs are passed to MyFirstGUI_OpeningFcn via varargin. 17 | % 18 | % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one 19 | % instance to run (singleton)". 20 | % 21 | % See also: GUIDE, GUIDATA, GUIHANDLES 22 | 23 | % Edit the above text to modify the response to help MyFirstGUI 24 | 25 | % Last Modified by GUIDE v2.5 21-Feb-2004 16:17:45 26 | 27 | % Begin initialization code - DO NOT EDIT 28 | gui_Singleton = 1; 29 | gui_State = struct('gui_Name', mfilename, ... 30 | 'gui_Singleton', gui_Singleton, ... 31 | 'gui_OpeningFcn', @MyFirstGUI_OpeningFcn, ... 32 | 'gui_OutputFcn', @MyFirstGUI_OutputFcn, ... 33 | 'gui_LayoutFcn', [] , ... 34 | 'gui_Callback', []); 35 | if nargin & isstr(varargin{1}) 36 | gui_State.gui_Callback = str2func(varargin{1}); 37 | end 38 | 39 | if nargout 40 | [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); 41 | else 42 | gui_mainfcn(gui_State, varargin{:}); 43 | end 44 | % End initialization code - DO NOT EDIT 45 | 46 | 47 | % --- Executes just before MyFirstGUI is made visible. 48 | function MyFirstGUI_OpeningFcn(hObject, eventdata, handles, varargin) 49 | % This function has no output args, see OutputFcn. 50 | % hObject handle to figure 51 | % eventdata reserved - to be defined in a future version of MATLAB 52 | % handles structure with handles and user data (see GUIDATA) 53 | % varargin command line arguments to MyFirstGUI (see VARARGIN) 54 | 55 | % Choose default command line output for MyFirstGUI 56 | handles.output = hObject; 57 | 58 | % Update handles structure 59 | guidata(hObject, handles); 60 | 61 | % UIWAIT makes MyFirstGUI wait for user response (see UIRESUME) 62 | % uiwait(handles.figure1); 63 | 64 | 65 | % --- Outputs from this function are returned to the command line. 66 | function varargout = MyFirstGUI_OutputFcn(hObject, eventdata, handles) 67 | % varargout cell array for returning output args (see VARARGOUT); 68 | % hObject handle to figure 69 | % eventdata reserved - to be defined in a future version of MATLAB 70 | % handles structure with handles and user data (see GUIDATA) 71 | 72 | % Get default command line output from handles structure 73 | varargout{1} = handles.output; 74 | 75 | 76 | % --- Executes on button press in MyFirstButton. 77 | function MyFirstButton_Callback(hObject, eventdata, handles) 78 | % hObject handle to MyFirstButton (see GCBO) 79 | % eventdata reserved - to be defined in a future version of MATLAB 80 | % handles structure with handles and user data (see GUIDATA) 81 | 82 | % Declare and initialize variable to store the count 83 | persistent count 84 | if isempty(count) 85 | count = 0; 86 | end 87 | 88 | % Update count 89 | count = count + 1; 90 | 91 | % Create new string 92 | str = sprintf('Total Clicks: %d',count); 93 | 94 | % Update the text field 95 | set (handles.MyFirstText,'String',str); 96 | 97 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap10/MyFirstGUI1/MyFirstGUI.fig: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/101Hub/Matlab101/07273f68f1147a110443aeb121fa10962234f298/assets/《Matlab编程》源码/chap10/MyFirstGUI1/MyFirstGUI.fig -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap10/MyFirstGUI1/MyFirstGUI.m: -------------------------------------------------------------------------------- 1 | function varargout = MyFirstGUI(varargin) 2 | % MYFIRSTGUI M-file for MyFirstGUI.fig 3 | % MYFIRSTGUI, by itself, creates a new MYFIRSTGUI or raises the existing 4 | % singleton*. 5 | % 6 | % H = MYFIRSTGUI returns the handle to a new MYFIRSTGUI or the handle to 7 | % the existing singleton*. 8 | % 9 | % MYFIRSTGUI('CALLBACK',hObject,eventData,handles,...) calls the local 10 | % function named CALLBACK in MYFIRSTGUI.M with the given input arguments. 11 | % 12 | % MYFIRSTGUI('Property','Value',...) creates a new MYFIRSTGUI or raises the 13 | % existing singleton*. Starting from the left, property value pairs are 14 | % applied to the GUI before MyFirstGUI_OpeningFunction gets called. An 15 | % unrecognized property name or invalid value makes property application 16 | % stop. All inputs are passed to MyFirstGUI_OpeningFcn via varargin. 17 | % 18 | % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one 19 | % instance to run (singleton)". 20 | % 21 | % See also: GUIDE, GUIDATA, GUIHANDLES 22 | 23 | % Edit the above text to modify the response to help MyFirstGUI 24 | 25 | % Last Modified by GUIDE v2.5 21-Feb-2004 16:17:45 26 | 27 | % Begin initialization code - DO NOT EDIT 28 | gui_Singleton = 1; 29 | gui_State = struct('gui_Name', mfilename, ... 30 | 'gui_Singleton', gui_Singleton, ... 31 | 'gui_OpeningFcn', @MyFirstGUI_OpeningFcn, ... 32 | 'gui_OutputFcn', @MyFirstGUI_OutputFcn, ... 33 | 'gui_LayoutFcn', [] , ... 34 | 'gui_Callback', []); 35 | if nargin & isstr(varargin{1}) 36 | gui_State.gui_Callback = str2func(varargin{1}); 37 | end 38 | 39 | if nargout 40 | [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); 41 | else 42 | gui_mainfcn(gui_State, varargin{:}); 43 | end 44 | % End initialization code - DO NOT EDIT 45 | 46 | 47 | % --- Executes just before MyFirstGUI is made visible. 48 | function MyFirstGUI_OpeningFcn(hObject, eventdata, handles, varargin) 49 | % This function has no output args, see OutputFcn. 50 | % hObject handle to figure 51 | % eventdata reserved - to be defined in a future version of MATLAB 52 | % handles structure with handles and user data (see GUIDATA) 53 | % varargin command line arguments to MyFirstGUI (see VARARGIN) 54 | 55 | % Choose default command line output for MyFirstGUI 56 | handles.output = hObject; 57 | 58 | % Update handles structure 59 | guidata(hObject, handles); 60 | 61 | % UIWAIT makes MyFirstGUI wait for user response (see UIRESUME) 62 | % uiwait(handles.figure1); 63 | 64 | 65 | % --- Outputs from this function are returned to the command line. 66 | function varargout = MyFirstGUI_OutputFcn(hObject, eventdata, handles) 67 | % varargout cell array for returning output args (see VARARGOUT); 68 | % hObject handle to figure 69 | % eventdata reserved - to be defined in a future version of MATLAB 70 | % handles structure with handles and user data (see GUIDATA) 71 | 72 | % Get default command line output from handles structure 73 | varargout{1} = handles.output; 74 | 75 | 76 | % --- Executes on button press in MyFirstButton. 77 | function MyFirstButton_Callback(hObject, eventdata, handles) 78 | % hObject handle to MyFirstButton (see GCBO) 79 | % eventdata reserved - to be defined in a future version of MATLAB 80 | % handles structure with handles and user data (see GUIDATA) 81 | 82 | % Create the count field if it does not exist 83 | if ~isfield(handles,'count') 84 | handles.count = 0; 85 | end 86 | 87 | % Update count 88 | handles.count = handles.count + 1; 89 | 90 | % Save the updated handles structure 91 | guidata(hObject, handles); 92 | 93 | % Create new string 94 | str = sprintf('Total Clicks: %d',handles.count); 95 | 96 | % Update the text field 97 | set (handles.MyFirstText,'String',str); 98 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap10/fig10-2.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/101Hub/Matlab101/07273f68f1147a110443aeb121fa10962234f298/assets/《Matlab编程》源码/chap10/fig10-2.ppt -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap10/fig10-7.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/101Hub/Matlab101/07273f68f1147a110443aeb121fa10962234f298/assets/《Matlab编程》源码/chap10/fig10-7.ppt -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap10/fig10-8.ppt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/101Hub/Matlab101/07273f68f1147a110443aeb121fa10962234f298/assets/《Matlab编程》源码/chap10/fig10-8.ppt -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap10/fig10_1.fig: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/101Hub/Matlab101/07273f68f1147a110443aeb121fa10962234f298/assets/《Matlab编程》源码/chap10/fig10_1.fig -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap10/fig10_1.m: -------------------------------------------------------------------------------- 1 | function varargout = fig10_1(varargin) 2 | % FIG10_1 Application M-file for fig10_1.fig 3 | % FIG = FIG10_1 launch fig10_1 GUI. 4 | % FIG10_1('callback_name', ...) invoke the named callback. 5 | 6 | % Last Modified by GUIDE v2.5 07-Mar-2004 20:46:39 7 | 8 | if nargin == 0 % LAUNCH GUI 9 | 10 | fig = openfig(mfilename,'reuse'); 11 | 12 | % Generate a structure of handles to pass to callbacks, and store it. 13 | handles = guihandles(fig); 14 | guidata(fig, handles); 15 | 16 | if nargout > 0 17 | varargout{1} = fig; 18 | end 19 | 20 | elseif ischar(varargin{1}) % INVOKE NAMED SUBFUNCTION OR CALLBACK 21 | 22 | try 23 | [varargout{1:nargout}] = feval(varargin{:}); % FEVAL switchyard 24 | catch 25 | disp(lasterr); 26 | end 27 | 28 | end 29 | 30 | 31 | %| ABOUT CALLBACKS: 32 | %| GUIDE automatically appends subfunction prototypes to this file, and 33 | %| sets objects' callback properties to call them through the FEVAL 34 | %| switchyard above. This comment describes that mechanism. 35 | %| 36 | %| Each callback subfunction declaration has the following form: 37 | %| (H, EVENTDATA, HANDLES, VARARGIN) 38 | %| 39 | %| The subfunction name is composed using the object's Tag and the 40 | %| callback type separated by '_', e.g. 'slider2_Callback', 41 | %| 'figure1_CloseRequestFcn', 'axis1_ButtondownFcn'. 42 | %| 43 | %| H is the callback object's handle (obtained using GCBO). 44 | %| 45 | %| EVENTDATA is empty, but reserved for future use. 46 | %| 47 | %| HANDLES is a structure containing handles of components in GUI using 48 | %| tags as fieldnames, e.g. handles.figure1, handles.slider2. This 49 | %| structure is created at GUI startup using GUIHANDLES and stored in 50 | %| the figure's application data using GUIDATA. A copy of the structure 51 | %| is passed to each callback. You can store additional information in 52 | %| this structure at GUI startup, and you can change the structure 53 | %| during callbacks. Call guidata(h, handles) after changing your 54 | %| copy to replace the stored original so that subsequent callbacks see 55 | %| the updates. Type "help guihandles" and "help guidata" for more 56 | %| information. 57 | %| 58 | %| VARARGIN contains any extra arguments you have passed to the 59 | %| callback. Specify the extra arguments by editing the callback 60 | %| property in the inspector. By default, GUIDE sets the property to: 61 | %| ('', gcbo, [], guidata(gcbo)) 62 | %| Add any extra arguments after the last argument, before the final 63 | %| closing parenthesis. 64 | 65 | 66 | 67 | % -------------------------------------------------------------------- 68 | function varargout = pushbutton1_Callback(h, eventdata, handles, varargin) 69 | % Stub for Callback of the uicontrol handles.pushbutton1. 70 | disp('pushbutton1 Callback not implemented yet.') 71 | 72 | 73 | % -------------------------------------------------------------------- 74 | function varargout = togglebutton1_Callback(h, eventdata, handles, varargin) 75 | % Stub for Callback of the uicontrol handles.togglebutton1. 76 | disp('togglebutton1 Callback not implemented yet.') 77 | 78 | 79 | % -------------------------------------------------------------------- 80 | function varargout = radiobutton1_Callback(h, eventdata, handles, varargin) 81 | % Stub for Callback of the uicontrol handles.radiobutton1. 82 | disp('radiobutton1 Callback not implemented yet.') 83 | 84 | 85 | % -------------------------------------------------------------------- 86 | function varargout = checkbox1_Callback(h, eventdata, handles, varargin) 87 | % Stub for Callback of the uicontrol handles.checkbox1. 88 | disp('checkbox1 Callback not implemented yet.') 89 | 90 | 91 | % -------------------------------------------------------------------- 92 | function varargout = edit1_Callback(h, eventdata, handles, varargin) 93 | % Stub for Callback of the uicontrol handles.edit1. 94 | disp('edit1 Callback not implemented yet.') 95 | 96 | 97 | % -------------------------------------------------------------------- 98 | function varargout = slider1_Callback(h, eventdata, handles, varargin) 99 | % Stub for Callback of the uicontrol handles.slider1. 100 | disp('slider1 Callback not implemented yet.') 101 | 102 | 103 | % -------------------------------------------------------------------- 104 | function varargout = listbox1_Callback(h, eventdata, handles, varargin) 105 | % Stub for Callback of the uicontrol handles.listbox1. 106 | disp('listbox1 Callback not implemented yet.') 107 | 108 | 109 | % -------------------------------------------------------------------- 110 | function varargout = radiobutton3_Callback(h, eventdata, handles, varargin) 111 | % Stub for Callback of the uicontrol handles.radiobutton3. 112 | disp('radiobutton3 Callback not implemented yet.') 113 | 114 | 115 | % --- Executes on button press in radiobutton2. 116 | function radiobutton2_Callback(hObject, eventdata, handles) 117 | % hObject handle to radiobutton2 (see GCBO) 118 | % eventdata reserved - to be defined in a future version of MATLAB 119 | % handles structure with handles and user data (see GUIDATA) 120 | 121 | % Hint: get(hObject,'Value') returns toggle state of radiobutton2 122 | 123 | 124 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap10/histGUI.fig: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/101Hub/Matlab101/07273f68f1147a110443aeb121fa10962234f298/assets/《Matlab编程》源码/chap10/histGUI.fig -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap10/histGUI/HistGUI1.m: -------------------------------------------------------------------------------- 1 | function varargout = histGUI1(varargin) 2 | % HISTGUI Application M-file for histGUI.fig 3 | % FIG = HISTGUI launch histGUI GUI. 4 | % HISTGUI('callback_name', ...) invoke the named callback. 5 | 6 | % Last Modified by GUIDE v2.0 29-Jul-2001 11:18:39 7 | 8 | if nargin == 0 % LAUNCH GUI 9 | 10 | fig = openfig(mfilename,'reuse'); 11 | 12 | % Generate a structure of handles to pass to callbacks. 13 | handles = guihandles(fig); 14 | 15 | % Add the number of bins to this structure. 16 | handles.nbins = str2num(get(handles.NBins,'String')); 17 | 18 | % Save handles structure 19 | guidata(fig, handles); 20 | 21 | if nargout > 0 22 | varargout{1} = fig; 23 | end 24 | 25 | elseif ischar(varargin{1}) % INVOKE NAMED SUBFUNCTION OR CALLBACK 26 | 27 | try 28 | [varargout{1:nargout}] = feval(varargin{:}); % FEVAL switchyard 29 | catch 30 | disp(lasterr); 31 | end 32 | 33 | end 34 | 35 | 36 | %| ABOUT CALLBACKS: 37 | %| GUIDE automatically appends subfunction prototypes to this file, and 38 | %| sets objects' callback properties to call them through the FEVAL 39 | %| switchyard above. This comment describes that mechanism. 40 | %| 41 | %| Each callback subfunction declaration has the following form: 42 | %| (H, EVENTDATA, HANDLES, VARARGIN) 43 | %| 44 | %| The subfunction name is composed using the object's Tag and the 45 | %| callback type separated by '_', e.g. 'slider2_Callback', 46 | %| 'figure1_CloseRequestFcn', 'axis1_ButtondownFcn'. 47 | %| 48 | %| H is the callback object's handle (obtained using GCBO). 49 | %| 50 | %| EVENTDATA is empty, but reserved for future use. 51 | %| 52 | %| HANDLES is a structure containing handles of components in GUI using 53 | %| tags as fieldnames, e.g. handles.figure1, handles.slider2. This 54 | %| structure is created at GUI startup using GUIHANDLES and stored in 55 | %| the figure's application data using GUIDATA. A copy of the structure 56 | %| is passed to each callback. You can store additional information in 57 | %| this structure at GUI startup, and you can change the structure 58 | %| during callbacks. Call guidata(h, handles) after changing your 59 | %| copy to replace the stored original so that subsequent callbacks see 60 | %| the updates. Type "help guihandles" and "help guidata" for more 61 | %| information. 62 | %| 63 | %| VARARGIN contains any extra arguments you have passed to the 64 | %| callback. Specify the extra arguments by editing the callback 65 | %| property in the inspector. By default, GUIDE sets the property to: 66 | %| ('', gcbo, [], guidata(gcbo)) 67 | %| Add any extra arguments after the last argument, before the final 68 | %| closing parenthesis. 69 | 70 | 71 | % -------------------------------------------------------------------- 72 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 73 | % Callback to open and read a file. 74 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 75 | function varargout = FileOpen1_Callback(h, eventdata, handles, varargin) 76 | 77 | % Get file name 78 | [filename,path] = uigetfile('*.dat','Load Data File'); 79 | if filename ~= 0 80 | 81 | % Read data 82 | x = textread([path filename],'%f'); 83 | 84 | % Save in handles structure 85 | handles.x = x; 86 | guidata(gcbf, handles); 87 | 88 | % Create histogram 89 | hist(handles.x,handles.nbins); 90 | 91 | % Set axis labels 92 | xlabel('\bfValue'); 93 | ylabel('\bfCount'); 94 | 95 | % Calculate statistics 96 | ave = mean(x); 97 | med = median(x); 98 | sd = std(x); 99 | n = length(x); 100 | 101 | % Update fields 102 | set (handles.MeanData,'String',sprintf('%7.2f',ave)); 103 | set (handles.MedianData,'String',sprintf('%7.2f',med)); 104 | set (handles.StdDevData,'String',sprintf('%7.2f',sd)); 105 | set (handles.TitleString,'String',['Histogram (N = ' int2str(n) ')']); 106 | 107 | end 108 | 109 | 110 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 111 | % Callback to exit. 112 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 113 | function varargout = FileExit1_Callback(h, eventdata, handles, varargin) 114 | close(gcbf); 115 | 116 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 117 | % Callback to set number of bins. 118 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 119 | function varargout = NBins_Callback(h, eventdata, handles, varargin) 120 | 121 | % Get number of bins, round to integer, and update field 122 | nbins = str2num(get(gcbo,'String')); 123 | nbins = round(nbins); 124 | if nbins < 1 125 | nbins = 1; 126 | end 127 | set (handles.NBins,'String',int2str(nbins)); 128 | 129 | % Save in handles structure 130 | handles.nbins = nbins; 131 | guidata(gcbf, handles); 132 | 133 | % Re-display data, if available 134 | if handles.nbins > 0 & ~isempty(handles.x) 135 | 136 | % Create histogram 137 | hist(handles.x,handles.nbins); 138 | 139 | end 140 | 141 | 142 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap10/histGUI/histGUI.fig: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/101Hub/Matlab101/07273f68f1147a110443aeb121fa10962234f298/assets/《Matlab编程》源码/chap10/histGUI/histGUI.fig -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap10/histGUI/histGUI1.fig: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/101Hub/Matlab101/07273f68f1147a110443aeb121fa10962234f298/assets/《Matlab编程》源码/chap10/histGUI/histGUI1.fig -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap10/histGUI1.fig: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/101Hub/Matlab101/07273f68f1147a110443aeb121fa10962234f298/assets/《Matlab编程》源码/chap10/histGUI1.fig -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap10/input1.dat: -------------------------------------------------------------------------------- 1 | 0 0 2 | 0.1000 0.0100 3 | 0.2000 0.0400 4 | 0.3000 0.0900 5 | 0.4000 0.1600 6 | 0.5000 0.2500 7 | 0.6000 0.3600 8 | 0.7000 0.4900 9 | 0.8000 0.6400 10 | 0.9000 0.8100 11 | 1.0000 1.0000 12 | 1.1000 1.2100 13 | 1.2000 1.4400 14 | 1.3000 1.6900 15 | 1.4000 1.9600 16 | 1.5000 2.2500 17 | 1.6000 2.5600 18 | 1.7000 2.8900 19 | 1.8000 3.2400 20 | 1.9000 3.6100 21 | 2.0000 4.0000 22 | 2.1000 4.4100 23 | 2.2000 4.8400 24 | 2.3000 5.2900 25 | 2.4000 5.7600 26 | 2.5000 6.2500 27 | 2.6000 6.7600 28 | 2.7000 7.2900 29 | 2.8000 7.8400 30 | 2.9000 8.4100 31 | 3.0000 9.0000 32 | 3.1000 9.6100 33 | 3.2000 10.2400 34 | 3.3000 10.8900 35 | 3.4000 11.5600 36 | 3.5000 12.2500 37 | 3.6000 12.9600 38 | 3.7000 13.6900 39 | 3.8000 14.4400 40 | 3.9000 15.2100 41 | 4.0000 16.0000 42 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap10/junk/MyFirstGUI.fig: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/101Hub/Matlab101/07273f68f1147a110443aeb121fa10962234f298/assets/《Matlab编程》源码/chap10/junk/MyFirstGUI.fig -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap10/junk/MyFirstGUI.m: -------------------------------------------------------------------------------- 1 | function varargout = MyFirstGUI(varargin) 2 | % MYFIRSTGUI M-file for MyFirstGUI.fig 3 | % MYFIRSTGUI, by itself, creates a new MYFIRSTGUI or raises the existing 4 | % singleton*. 5 | % 6 | % H = MYFIRSTGUI returns the handle to a new MYFIRSTGUI or the handle to 7 | % the existing singleton*. 8 | % 9 | % MYFIRSTGUI('CALLBACK',hObject,eventData,handles,...) calls the local 10 | % function named CALLBACK in MYFIRSTGUI.M with the given input arguments. 11 | % 12 | % MYFIRSTGUI('Property','Value',...) creates a new MYFIRSTGUI or raises the 13 | % existing singleton*. Starting from the left, property value pairs are 14 | % applied to the GUI before MyFirstGUI_OpeningFunction gets called. An 15 | % unrecognized property name or invalid value makes property application 16 | % stop. All inputs are passed to MyFirstGUI_OpeningFcn via varargin. 17 | % 18 | % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one 19 | % instance to run (singleton)". 20 | % 21 | % See also: GUIDE, GUIDATA, GUIHANDLES 22 | 23 | % Edit the above text to modify the response to help MyFirstGUI 24 | 25 | % Last Modified by GUIDE v2.5 21-May-2007 18:02:51 26 | 27 | % Begin initialization code - DO NOT EDIT 28 | gui_Singleton = 1; 29 | gui_State = struct('gui_Name', mfilename, ... 30 | 'gui_Singleton', gui_Singleton, ... 31 | 'gui_OpeningFcn', @MyFirstGUI_OpeningFcn, ... 32 | 'gui_OutputFcn', @MyFirstGUI_OutputFcn, ... 33 | 'gui_LayoutFcn', [] , ... 34 | 'gui_Callback', []); 35 | if nargin && ischar(varargin{1}) 36 | gui_State.gui_Callback = str2func(varargin{1}); 37 | end 38 | 39 | if nargout 40 | [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); 41 | else 42 | gui_mainfcn(gui_State, varargin{:}); 43 | end 44 | % End initialization code - DO NOT EDIT 45 | 46 | 47 | % --- Executes just before MyFirstGUI is made visible. 48 | function MyFirstGUI_OpeningFcn(hObject, eventdata, handles, varargin) 49 | % This function has no output args, see OutputFcn. 50 | % hObject handle to figure 51 | % eventdata reserved - to be defined in a future version of MATLAB 52 | % handles structure with handles and user data (see GUIDATA) 53 | % varargin command line arguments to MyFirstGUI (see VARARGIN) 54 | 55 | % Choose default command line output for MyFirstGUI 56 | handles.output = hObject; 57 | 58 | % Update handles structure 59 | guidata(hObject, handles); 60 | 61 | % UIWAIT makes MyFirstGUI wait for user response (see UIRESUME) 62 | % uiwait(handles.figure1); 63 | 64 | 65 | % --- Outputs from this function are returned to the command line. 66 | function varargout = MyFirstGUI_OutputFcn(hObject, eventdata, handles) 67 | % varargout cell array for returning output args (see VARARGOUT); 68 | % hObject handle to figure 69 | % eventdata reserved - to be defined in a future version of MATLAB 70 | % handles structure with handles and user data (see GUIDATA) 71 | 72 | % Get default command line output from handles structure 73 | varargout{1} = handles.output; 74 | 75 | 76 | % --- Executes on button press in MyFirstButton. 77 | function MyFirstButton_Callback(hObject, eventdata, handles) 78 | % hObject handle to MyFirstButton (see GCBO) 79 | % eventdata reserved - to be defined in a future version of MATLAB 80 | % handles structure with handles and user data (see GUIDATA) 81 | 82 | 83 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap10/menu_test.fig: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/101Hub/Matlab101/07273f68f1147a110443aeb121fa10962234f298/assets/《Matlab编程》源码/chap10/menu_test.fig -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap10/menu_test.m: -------------------------------------------------------------------------------- 1 | function varargout = menu_test(varargin) 2 | % MENU_TEST M-file for menu_test.fig 3 | % MENU_TEST, by itself, creates a new MENU_TEST or raises the existing 4 | % singleton*. 5 | % 6 | % H = MENU_TEST returns the handle to a new MENU_TEST or the handle to 7 | % the existing singleton*. 8 | % 9 | % MENU_TEST('CALLBACK',hObject,eventData,handles,...) calls the local 10 | % function named CALLBACK in MENU_TEST.M with the given input arguments. 11 | % 12 | % MENU_TEST('Property','Value',...) creates a new MENU_TEST or raises the 13 | % existing singleton*. Starting from the left, property value pairs are 14 | % applied to the GUI before menu_test_OpeningFunction gets called. An 15 | % unrecognized property name or invalid value makes property application 16 | % stop. All inputs are passed to menu_test_OpeningFcn via varargin. 17 | % 18 | % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one 19 | % instance to run (singleton)". 20 | % 21 | % See also: GUIDE, GUIDATA, GUIHANDLES 22 | 23 | % Edit the above text to modify the response to help menu_test 24 | 25 | % Last Modified by GUIDE v2.5 27-Mar-2004 18:11:23 26 | 27 | % Begin initialization code - DO NOT EDIT 28 | gui_Singleton = 1; 29 | gui_State = struct('gui_Name', mfilename, ... 30 | 'gui_Singleton', gui_Singleton, ... 31 | 'gui_OpeningFcn', @menu_test_OpeningFcn, ... 32 | 'gui_OutputFcn', @menu_test_OutputFcn, ... 33 | 'gui_LayoutFcn', [] , ... 34 | 'gui_Callback', []); 35 | if nargin && ischar(varargin{1}) 36 | gui_State.gui_Callback = str2func(varargin{1}); 37 | end 38 | 39 | if nargout 40 | [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); 41 | else 42 | gui_mainfcn(gui_State, varargin{:}); 43 | end 44 | % End initialization code - DO NOT EDIT 45 | 46 | 47 | % --- Executes just before menu_test is made visible. 48 | function menu_test_OpeningFcn(hObject, eventdata, handles, varargin) 49 | % This function has no output args, see OutputFcn. 50 | % hObject handle to figure 51 | % eventdata reserved - to be defined in a future version of MATLAB 52 | % handles structure with handles and user data (see GUIDATA) 53 | % varargin command line arguments to menu_test (see VARARGIN) 54 | 55 | % Choose default command line output for menu_test 56 | handles.output = hObject; 57 | 58 | % Update handles structure 59 | guidata(hObject, handles); 60 | 61 | % UIWAIT makes menu_test wait for user response (see UIRESUME) 62 | % uiwait(handles.figure1); 63 | 64 | 65 | % --- Outputs from this function are returned to the command line. 66 | function varargout = menu_test_OutputFcn(hObject, eventdata, handles) 67 | % varargout cell array for returning output args (see VARARGOUT); 68 | % hObject handle to figure 69 | % eventdata reserved - to be defined in a future version of MATLAB 70 | % handles structure with handles and user data (see GUIDATA) 71 | 72 | % Get default command line output from handles structure 73 | varargout{1} = handles.output; 74 | 75 | 76 | % -------------------------------------------------------------------- 77 | function Open_Callback(hObject, eventdata, handles) 78 | % hObject handle to Open (see GCBO) 79 | % eventdata reserved - to be defined in a future version of MATLAB 80 | % handles structure with handles and user data (see GUIDATA) 81 | 82 | 83 | % -------------------------------------------------------------------- 84 | function Save_Callback(hObject, eventdata, handles) 85 | % hObject handle to Save (see GCBO) 86 | % eventdata reserved - to be defined in a future version of MATLAB 87 | % handles structure with handles and user data (see GUIDATA) 88 | 89 | 90 | % -------------------------------------------------------------------- 91 | function SaveAs_Callback(hObject, eventdata, handles) 92 | % hObject handle to SaveAs (see GCBO) 93 | % eventdata reserved - to be defined in a future version of MATLAB 94 | % handles structure with handles and user data (see GUIDATA) 95 | 96 | 97 | % -------------------------------------------------------------------- 98 | function Exit_Callback(hObject, eventdata, handles) 99 | % hObject handle to Exit (see GCBO) 100 | % eventdata reserved - to be defined in a future version of MATLAB 101 | % handles structure with handles and user data (see GUIDATA) 102 | 103 | 104 | % -------------------------------------------------------------------- 105 | function File_Callback(hObject, eventdata, handles) 106 | % hObject handle to File (see GCBO) 107 | % eventdata reserved - to be defined in a future version of MATLAB 108 | % handles structure with handles and user data (see GUIDATA) 109 | 110 | 111 | % -------------------------------------------------------------------- 112 | function Edit_Callback(hObject, eventdata, handles) 113 | % hObject handle to Edit (see GCBO) 114 | % eventdata reserved - to be defined in a future version of MATLAB 115 | % handles structure with handles and user data (see GUIDATA) 116 | 117 | 118 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap10/panels.fig: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/101Hub/Matlab101/07273f68f1147a110443aeb121fa10962234f298/assets/《Matlab编程》源码/chap10/panels.fig -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap10/panels.m: -------------------------------------------------------------------------------- 1 | function varargout = panels(varargin) 2 | % PANELS M-file for panels.fig 3 | % PANELS, by itself, creates a new PANELS or raises the existing 4 | % singleton*. 5 | % 6 | % H = PANELS returns the handle to a new PANELS or the handle to 7 | % the existing singleton*. 8 | % 9 | % PANELS('CALLBACK',hObject,eventData,handles,...) calls the local 10 | % function named CALLBACK in PANELS.M with the given input arguments. 11 | % 12 | % PANELS('Property','Value',...) creates a new PANELS or raises the 13 | % existing singleton*. Starting from the left, property value pairs are 14 | % applied to the GUI before panels_OpeningFunction gets called. An 15 | % unrecognized property name or invalid value makes property application 16 | % stop. All inputs are passed to panels_OpeningFcn via varargin. 17 | % 18 | % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one 19 | % instance to run (singleton)". 20 | % 21 | % See also: GUIDE, GUIDATA, GUIHANDLES 22 | 23 | % Edit the above text to modify the response to help panels 24 | 25 | % Last Modified by GUIDE v2.5 25-Mar-2002 12:19:15 26 | 27 | % Begin initialization code - DO NOT EDIT 28 | gui_Singleton = 1; 29 | gui_State = struct('gui_Name', mfilename, ... 30 | 'gui_Singleton', gui_Singleton, ... 31 | 'gui_OpeningFcn', @panels_OpeningFcn, ... 32 | 'gui_OutputFcn', @panels_OutputFcn, ... 33 | 'gui_LayoutFcn', [] , ... 34 | 'gui_Callback', []); 35 | if nargin && ischar(varargin{1}) 36 | gui_State.gui_Callback = str2func(varargin{1}); 37 | end 38 | 39 | if nargout 40 | [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); 41 | else 42 | gui_mainfcn(gui_State, varargin{:}); 43 | end 44 | % End initialization code - DO NOT EDIT 45 | 46 | 47 | % --- Executes just before panels is made visible. 48 | function panels_OpeningFcn(hObject, eventdata, handles, varargin) 49 | % This function has no output args, see OutputFcn. 50 | % hObject handle to figure 51 | % eventdata reserved - to be defined in a future version of MATLAB 52 | % handles structure with handles and user data (see GUIDATA) 53 | % varargin command line arguments to panels (see VARARGIN) 54 | 55 | % Choose default command line output for panels 56 | handles.output = hObject; 57 | 58 | % Update handles structure 59 | guidata(hObject, handles); 60 | 61 | % UIWAIT makes panels wait for user response (see UIRESUME) 62 | % uiwait(handles.figure1); 63 | 64 | 65 | % --- Outputs from this function are returned to the command line. 66 | function varargout = panels_OutputFcn(hObject, eventdata, handles) 67 | % varargout cell array for returning output args (see VARARGOUT); 68 | % hObject handle to figure 69 | % eventdata reserved - to be defined in a future version of MATLAB 70 | % handles structure with handles and user data (see GUIDATA) 71 | 72 | % Get default command line output from handles structure 73 | varargout{1} = handles.output; 74 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap10/plot_function.fig: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/101Hub/Matlab101/07273f68f1147a110443aeb121fa10962234f298/assets/《Matlab编程》源码/chap10/plot_function.fig -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap10/plot_line.fig: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/101Hub/Matlab101/07273f68f1147a110443aeb121fa10962234f298/assets/《Matlab编程》源码/chap10/plot_line.fig -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap10/temp_conversion/temp_conversion.fig: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/101Hub/Matlab101/07273f68f1147a110443aeb121fa10962234f298/assets/《Matlab编程》源码/chap10/temp_conversion/temp_conversion.fig -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap10/temp_conversion/to_c.m: -------------------------------------------------------------------------------- 1 | function deg_c = to_c(deg_f) 2 | 3 | % Convert degrees Fahrenheit to degrees C. 4 | deg_c = (5/9) * (deg_f - 32); 5 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap10/temp_conversion/to_f.m: -------------------------------------------------------------------------------- 1 | function deg_f = to_f(deg_c) 2 | 3 | % Convert degrees Celsius to degrees Fahrenheit. 4 | deg_f = (9/5) * deg_c + 32; 5 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap10/test_button_group.fig: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/101Hub/Matlab101/07273f68f1147a110443aeb121fa10962234f298/assets/《Matlab编程》源码/chap10/test_button_group.fig -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap10/test_button_group.m: -------------------------------------------------------------------------------- 1 | function varargout = test_button_group(varargin) 2 | % TEST_BUTTON_GROUP M-file for test_button_group.fig 3 | % TEST_BUTTON_GROUP, by itself, creates a new TEST_BUTTON_GROUP or raises the existing 4 | % singleton*. 5 | % 6 | % H = TEST_BUTTON_GROUP returns the handle to a new TEST_BUTTON_GROUP or the handle to 7 | % the existing singleton*. 8 | % 9 | % TEST_BUTTON_GROUP('CALLBACK',hObject,eventData,handles,...) calls the local 10 | % function named CALLBACK in TEST_BUTTON_GROUP.M with the given input arguments. 11 | % 12 | % TEST_BUTTON_GROUP('Property','Value',...) creates a new TEST_BUTTON_GROUP or raises the 13 | % existing singleton*. Starting from the left, property value pairs are 14 | % applied to the GUI before test_button_group_OpeningFunction gets called. An 15 | % unrecognized property name or invalid value makes property application 16 | % stop. All inputs are passed to test_button_group_OpeningFcn via varargin. 17 | % 18 | % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one 19 | % instance to run (singleton)". 20 | % 21 | % See also: GUIDE, GUIDATA, GUIHANDLES 22 | 23 | % Edit the above text to modify the response to help test_button_group 24 | 25 | % Last Modified by GUIDE v2.5 25-Mar-2002 12:19:15 26 | 27 | % Begin initialization code - DO NOT EDIT 28 | gui_Singleton = 1; 29 | gui_State = struct('gui_Name', mfilename, ... 30 | 'gui_Singleton', gui_Singleton, ... 31 | 'gui_OpeningFcn', @test_button_group_OpeningFcn, ... 32 | 'gui_OutputFcn', @test_button_group_OutputFcn, ... 33 | 'gui_LayoutFcn', [] , ... 34 | 'gui_Callback', []); 35 | if nargin && ischar(varargin{1}) 36 | gui_State.gui_Callback = str2func(varargin{1}); 37 | end 38 | 39 | if nargout 40 | [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); 41 | else 42 | gui_mainfcn(gui_State, varargin{:}); 43 | end 44 | % End initialization code - DO NOT EDIT 45 | 46 | 47 | % --- Executes just before test_button_group is made visible. 48 | function test_button_group_OpeningFcn(hObject, eventdata, handles, varargin) 49 | % This function has no output args, see OutputFcn. 50 | % hObject handle to figure 51 | % eventdata reserved - to be defined in a future version of MATLAB 52 | % handles structure with handles and user data (see GUIDATA) 53 | % varargin command line arguments to test_button_group (see VARARGIN) 54 | 55 | % Choose default command line output for test_button_group 56 | handles.output = hObject; 57 | 58 | % Update handles structure 59 | guidata(hObject, handles); 60 | 61 | % UIWAIT makes test_button_group wait for user response (see UIRESUME) 62 | % uiwait(handles.figure1); 63 | 64 | 65 | % --- Outputs from this function are returned to the command line. 66 | function varargout = test_button_group_OutputFcn(hObject, eventdata, handles) 67 | % varargout cell array for returning output args (see VARARGOUT); 68 | % hObject handle to figure 69 | % eventdata reserved - to be defined in a future version of MATLAB 70 | % handles structure with handles and user data (see GUIDATA) 71 | 72 | % Get default command line output from handles structure 73 | varargout{1} = handles.output; 74 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap10/test_button_group1.m: -------------------------------------------------------------------------------- 1 | function test_button_group1 2 | fig = figure; 3 | pos = get(gcf,'Position'); 4 | pos(3) = 300; 5 | pos(4) = 200; 6 | set(gcf,'Position',pos,'NumberTitle','off','MenuBar','none', ... 7 | 'Name','Test Button Group','ToolBar','none'); 8 | h = uibuttongroup('visible','off','Position',[0.1 0.1 0.8 0.8],... 9 | 'Title','Button Group'); 10 | u0 = uicontrol('Style','Radio','String','Option 1',... 11 | 'Units','normalized', ... 12 | 'pos',[0.1 0.7 0.8 0.1],'parent',h,'HandleVisibility','off'); 13 | u1 = uicontrol('Style','Radio','String','Option 2',... 14 | 'Units','normalized', ... 15 | 'pos',[0.1 0.4 0.8 0.1],'parent',h,'HandleVisibility','off'); 16 | u2 = uicontrol('Style','Radio','String','Option 3',... 17 | 'Units','normalized', ... 18 | 'pos',[0.1 0.1 0.8 0.1],'parent',h,'HandleVisibility','off'); 19 | set(h,'SelectionChangeFcn',@selcbk); 20 | set(h,'SelectedObject',[]); % No selection 21 | set(h,'Visible','on'); 22 | end 23 | 24 | function selcbk(source,eventdata) 25 | disp(source); 26 | disp([eventdata.EventName,' ',... 27 | get(eventdata.OldValue,'String'),' ', ... 28 | get(eventdata.NewValue,'String')]); 29 | disp(get(get(source,'SelectedObject'),'String')); 30 | end 31 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap10/test_checkbox.fig: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/101Hub/Matlab101/07273f68f1147a110443aeb121fa10962234f298/assets/《Matlab编程》源码/chap10/test_checkbox.fig -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap10/test_checkbox.m: -------------------------------------------------------------------------------- 1 | function varargout = test_checkbox(varargin) 2 | % TEST_CHECKBOX M-file for test_checkbox.fig 3 | % TEST_CHECKBOX, by itself, creates a new TEST_CHECKBOX or raises the existing 4 | % singleton*. 5 | % 6 | % H = TEST_CHECKBOX returns the handle to a new TEST_CHECKBOX or the handle to 7 | % the existing singleton*. 8 | % 9 | % TEST_CHECKBOX('CALLBACK',hObject,eventData,handles,...) calls the local 10 | % function named CALLBACK in TEST_CHECKBOX.M with the given input arguments. 11 | % 12 | % TEST_CHECKBOX('Property','Value',...) creates a new TEST_CHECKBOX or raises the 13 | % existing singleton*. Starting from the left, property value pairs are 14 | % applied to the GUI before test_checkbox_OpeningFunction gets called. An 15 | % unrecognized property name or invalid value makes property application 16 | % stop. All inputs are passed to test_checkbox_OpeningFcn via varargin. 17 | % 18 | % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one 19 | % instance to run (singleton)". 20 | % 21 | % See also: GUIDE, GUIDATA, GUIHANDLES 22 | 23 | % Edit the above text to modify the response to help test_checkbox 24 | 25 | % Last Modified by GUIDE v2.5 13-Mar-2004 16:38:26 26 | 27 | % Begin initialization code - DO NOT EDIT 28 | gui_Singleton = 1; 29 | gui_State = struct('gui_Name', mfilename, ... 30 | 'gui_Singleton', gui_Singleton, ... 31 | 'gui_OpeningFcn', @test_checkbox_OpeningFcn, ... 32 | 'gui_OutputFcn', @test_checkbox_OutputFcn, ... 33 | 'gui_LayoutFcn', [] , ... 34 | 'gui_Callback', []); 35 | if nargin && ischar(varargin{1}) 36 | gui_State.gui_Callback = str2func(varargin{1}); 37 | end 38 | 39 | if nargout 40 | [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); 41 | else 42 | gui_mainfcn(gui_State, varargin{:}); 43 | end 44 | % End initialization code - DO NOT EDIT 45 | 46 | 47 | % --- Executes just before test_checkbox is made visible. 48 | function test_checkbox_OpeningFcn(hObject, eventdata, handles, varargin) 49 | % This function has no output args, see OutputFcn. 50 | % hObject handle to figure 51 | % eventdata reserved - to be defined in a future version of MATLAB 52 | % handles structure with handles and user data (see GUIDATA) 53 | % varargin command line arguments to test_checkbox (see VARARGIN) 54 | 55 | % Choose default command line output for test_checkbox 56 | handles.output = hObject; 57 | 58 | % Update handles structure 59 | guidata(hObject, handles); 60 | 61 | % UIWAIT makes test_checkbox wait for user response (see UIRESUME) 62 | % uiwait(handles.figure1); 63 | 64 | 65 | % --- Outputs from this function are returned to the command line. 66 | function varargout = test_checkbox_OutputFcn(hObject, eventdata, handles) 67 | % varargout cell array for returning output args (see VARARGOUT); 68 | % hObject handle to figure 69 | % eventdata reserved - to be defined in a future version of MATLAB 70 | % handles structure with handles and user data (see GUIDATA) 71 | 72 | % Get default command line output from handles structure 73 | varargout{1} = handles.output; 74 | 75 | 76 | % --- Executes on button press in CheckBox. 77 | function CheckBox_Callback(hObject, eventdata, handles) 78 | % hObject handle to CheckBox (see GCBO) 79 | % eventdata reserved - to be defined in a future version of MATLAB 80 | % handles structure with handles and user data (see GUIDATA) 81 | 82 | % Find the state of the checkbox 83 | state = get(handles.CheckBox,'Value'); 84 | 85 | % Place the value into the text field 86 | if state == 0 87 | set (handles.TextBox,'String','Off'); 88 | else 89 | set (handles.TextBox,'String','On'); 90 | end 91 | 92 | 93 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap10/test_edit.fig: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/101Hub/Matlab101/07273f68f1147a110443aeb121fa10962234f298/assets/《Matlab编程》源码/chap10/test_edit.fig -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap10/test_edit.m: -------------------------------------------------------------------------------- 1 | function varargout = test_edit(varargin) 2 | % TEST_EDIT M-file for test_edit.fig 3 | % TEST_EDIT, by itself, creates a new TEST_EDIT or raises the existing 4 | % singleton*. 5 | % 6 | % H = TEST_EDIT returns the handle to a new TEST_EDIT or the handle to 7 | % the existing singleton*. 8 | % 9 | % TEST_EDIT('CALLBACK',hObject,eventData,handles,...) calls the local 10 | % function named CALLBACK in TEST_EDIT.M with the given input arguments. 11 | % 12 | % TEST_EDIT('Property','Value',...) creates a new TEST_EDIT or raises the 13 | % existing singleton*. Starting from the left, property value pairs are 14 | % applied to the GUI before test_edit_OpeningFunction gets called. An 15 | % unrecognized property name or invalid value makes property application 16 | % stop. All inputs are passed to test_edit_OpeningFcn via varargin. 17 | % 18 | % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one 19 | % instance to run (singleton)". 20 | % 21 | % See also: GUIDE, GUIDATA, GUIHANDLES 22 | 23 | % Edit the above text to modify the response to help test_edit 24 | 25 | % Last Modified by GUIDE v2.5 22-Feb-2004 17:52:02 26 | 27 | % Begin initialization code - DO NOT EDIT 28 | gui_Singleton = 1; 29 | gui_State = struct('gui_Name', mfilename, ... 30 | 'gui_Singleton', gui_Singleton, ... 31 | 'gui_OpeningFcn', @test_edit_OpeningFcn, ... 32 | 'gui_OutputFcn', @test_edit_OutputFcn, ... 33 | 'gui_LayoutFcn', [] , ... 34 | 'gui_Callback', []); 35 | if nargin & isstr(varargin{1}) 36 | gui_State.gui_Callback = str2func(varargin{1}); 37 | end 38 | 39 | if nargout 40 | [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); 41 | else 42 | gui_mainfcn(gui_State, varargin{:}); 43 | end 44 | % End initialization code - DO NOT EDIT 45 | 46 | 47 | % --- Executes just before test_edit is made visible. 48 | function test_edit_OpeningFcn(hObject, eventdata, handles, varargin) 49 | % This function has no output args, see OutputFcn. 50 | % hObject handle to figure 51 | % eventdata reserved - to be defined in a future version of MATLAB 52 | % handles structure with handles and user data (see GUIDATA) 53 | % varargin command line arguments to test_edit (see VARARGIN) 54 | 55 | % Choose default command line output for test_edit 56 | handles.output = hObject; 57 | 58 | % Update handles structure 59 | guidata(hObject, handles); 60 | 61 | % UIWAIT makes test_edit wait for user response (see UIRESUME) 62 | % uiwait(handles.figure1); 63 | 64 | 65 | % --- Outputs from this function are returned to the command line. 66 | function varargout = test_edit_OutputFcn(hObject, eventdata, handles) 67 | % varargout cell array for returning output args (see VARARGOUT); 68 | % hObject handle to figure 69 | % eventdata reserved - to be defined in a future version of MATLAB 70 | % handles structure with handles and user data (see GUIDATA) 71 | 72 | % Get default command line output from handles structure 73 | varargout{1} = handles.output; 74 | 75 | 76 | function EditBox_Callback(hObject, eventdata, handles) 77 | % hObject handle to EditBox (see GCBO) 78 | % eventdata reserved - to be defined in a future version of MATLAB 79 | % handles structure with handles and user data (see GUIDATA) 80 | 81 | % Hints: get(hObject,'String') returns contents of EditBox as text 82 | % str2double(get(hObject,'String')) returns contents of EditBox as a double 83 | 84 | % Find the value typed into the edit box 85 | str = get (handles.EditBox,'String'); 86 | 87 | % Place the value into the text field 88 | set (handles.TextBox,'String',str); 89 | 90 | 91 | % --- Executes during object creation, after setting all properties. 92 | function EditBox_CreateFcn(hObject, eventdata, handles) 93 | % hObject handle to EditBox (see GCBO) 94 | % eventdata reserved - to be defined in a future version of MATLAB 95 | % handles empty - handles not created until after all CreateFcns called 96 | 97 | % Hint: edit controls usually have a white background on Windows. 98 | % See ISPC and COMPUTER. 99 | if ispc 100 | set(hObject,'BackgroundColor','white'); 101 | else 102 | set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor')); 103 | end 104 | 105 | 106 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap10/test_edit2.fig: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/101Hub/Matlab101/07273f68f1147a110443aeb121fa10962234f298/assets/《Matlab编程》源码/chap10/test_edit2.fig -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap10/test_edit2.m: -------------------------------------------------------------------------------- 1 | function varargout = test_edit2(varargin) 2 | % TEST_EDIT2 M-file for test_edit2.fig 3 | % TEST_EDIT2, by itself, creates a new TEST_EDIT2 or raises the existing 4 | % singleton*. 5 | % 6 | % H = TEST_EDIT2 returns the handle to a new TEST_EDIT2 or the handle to 7 | % the existing singleton*. 8 | % 9 | % TEST_EDIT2('CALLBACK',hObject,eventData,handles,...) calls the local 10 | % function named CALLBACK in TEST_EDIT2.M with the given input arguments. 11 | % 12 | % TEST_EDIT2('Property','Value',...) creates a new TEST_EDIT2 or raises the 13 | % existing singleton*. Starting from the left, property value pairs are 14 | % applied to the GUI before test_edit2_OpeningFunction gets called. An 15 | % unrecognized property name or invalid value makes property application 16 | % stop. All inputs are passed to test_edit2_OpeningFcn via varargin. 17 | % 18 | % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one 19 | % instance to run (singleton)". 20 | % 21 | % See also: GUIDE, GUIDATA, GUIHANDLES 22 | 23 | % Edit the above text to modify the response to help test_edit2 24 | 25 | % Last Modified by GUIDE v2.5 13-Mar-2004 14:59:53 26 | 27 | % Begin initialization code - DO NOT EDIT 28 | gui_Singleton = 1; 29 | gui_State = struct('gui_Name', mfilename, ... 30 | 'gui_Singleton', gui_Singleton, ... 31 | 'gui_OpeningFcn', @test_edit2_OpeningFcn, ... 32 | 'gui_OutputFcn', @test_edit2_OutputFcn, ... 33 | 'gui_LayoutFcn', [] , ... 34 | 'gui_Callback', []); 35 | if nargin & isstr(varargin{1}) 36 | gui_State.gui_Callback = str2func(varargin{1}); 37 | end 38 | 39 | if nargout 40 | [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); 41 | else 42 | gui_mainfcn(gui_State, varargin{:}); 43 | end 44 | % End initialization code - DO NOT EDIT 45 | 46 | 47 | % --- Executes just before test_edit2 is made visible. 48 | function test_edit2_OpeningFcn(hObject, eventdata, handles, varargin) 49 | % This function has no output args, see OutputFcn. 50 | % hObject handle to figure 51 | % eventdata reserved - to be defined in a future version of MATLAB 52 | % handles structure with handles and user data (see GUIDATA) 53 | % varargin command line arguments to test_edit2 (see VARARGIN) 54 | 55 | % Choose default command line output for test_edit2 56 | handles.output = hObject; 57 | 58 | % Update handles structure 59 | guidata(hObject, handles); 60 | 61 | % UIWAIT makes test_edit2 wait for user response (see UIRESUME) 62 | % uiwait(handles.figure1); 63 | 64 | 65 | % --- Outputs from this function are returned to the command line. 66 | function varargout = test_edit2_OutputFcn(hObject, eventdata, handles) 67 | % varargout cell array for returning output args (see VARARGOUT); 68 | % hObject handle to figure 69 | % eventdata reserved - to be defined in a future version of MATLAB 70 | % handles structure with handles and user data (see GUIDATA) 71 | 72 | % Get default command line output from handles structure 73 | varargout{1} = handles.output; 74 | 75 | 76 | function EditBox2_Callback(hObject, eventdata, handles) 77 | % hObject handle to EditBox2 (see GCBO) 78 | % eventdata reserved - to be defined in a future version of MATLAB 79 | % handles structure with handles and user data (see GUIDATA) 80 | 81 | % Hints: get(hObject,'String') returns contents of EditBox2 as text 82 | % str2double(get(hObject,'String')) returns contents of EditBox2 as a double 83 | 84 | % Find the value typed into the edit box 85 | str = get (handles.EditBox2,'String'); 86 | 87 | % Place the value into the text field 88 | set (handles.TextBox2,'String',str); 89 | 90 | 91 | % --- Executes during object creation, after setting all properties. 92 | function EditBox2_CreateFcn(hObject, eventdata, handles) 93 | % hObject handle to EditBox2 (see GCBO) 94 | % eventdata reserved - to be defined in a future version of MATLAB 95 | % handles empty - handles not created until after all CreateFcns called 96 | 97 | % Hint: edit controls usually have a white background on Windows. 98 | % See ISPC and COMPUTER. 99 | if ispc 100 | set(hObject,'BackgroundColor','white'); 101 | else 102 | set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor')); 103 | end 104 | 105 | 106 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap10/test_listbox_1.fig: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/101Hub/Matlab101/07273f68f1147a110443aeb121fa10962234f298/assets/《Matlab编程》源码/chap10/test_listbox_1.fig -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap10/test_listbox_1.m: -------------------------------------------------------------------------------- 1 | function varargout = test_listbox_1(varargin) 2 | % TEST_LISTBOX_1 M-file for test_listbox_1.fig 3 | % TEST_LISTBOX_1, by itself, creates a new TEST_LISTBOX_1 or raises the existing 4 | % singleton*. 5 | % 6 | % H = TEST_LISTBOX_1 returns the handle to a new TEST_LISTBOX_1 or the handle to 7 | % the existing singleton*. 8 | % 9 | % TEST_LISTBOX_1('CALLBACK',hObject,eventData,handles,...) calls the local 10 | % function named CALLBACK in TEST_LISTBOX_1.M with the given input arguments. 11 | % 12 | % TEST_LISTBOX_1('Property','Value',...) creates a new TEST_LISTBOX_1 or raises the 13 | % existing singleton*. Starting from the left, property value pairs are 14 | % applied to the GUI before test_listbox_1_OpeningFunction gets called. An 15 | % unrecognized property name or invalid value makes property application 16 | % stop. All inputs are passed to test_listbox_1_OpeningFcn via varargin. 17 | % 18 | % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one 19 | % instance to run (singleton)". 20 | % 21 | % See also: GUIDE, GUIDATA, GUIHANDLES 22 | 23 | % Edit the above text to modify the response to help test_listbox_1 24 | 25 | % Last Modified by GUIDE v2.5 13-Mar-2004 17:14:36 26 | 27 | % Begin initialization code - DO NOT EDIT 28 | gui_Singleton = 1; 29 | gui_State = struct('gui_Name', mfilename, ... 30 | 'gui_Singleton', gui_Singleton, ... 31 | 'gui_OpeningFcn', @test_listbox_1_OpeningFcn, ... 32 | 'gui_OutputFcn', @test_listbox_1_OutputFcn, ... 33 | 'gui_LayoutFcn', [] , ... 34 | 'gui_Callback', []); 35 | if nargin && ischar(varargin{1}) 36 | gui_State.gui_Callback = str2func(varargin{1}); 37 | end 38 | 39 | if nargout 40 | [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); 41 | else 42 | gui_mainfcn(gui_State, varargin{:}); 43 | end 44 | % End initialization code - DO NOT EDIT 45 | 46 | 47 | % --- Executes just before test_listbox_1 is made visible. 48 | function test_listbox_1_OpeningFcn(hObject, eventdata, handles, varargin) 49 | % This function has no output args, see OutputFcn. 50 | % hObject handle to figure 51 | % eventdata reserved - to be defined in a future version of MATLAB 52 | % handles structure with handles and user data (see GUIDATA) 53 | % varargin command line arguments to test_listbox_1 (see VARARGIN) 54 | 55 | % Choose default command line output for test_listbox_1 56 | handles.output = hObject; 57 | 58 | % Update handles structure 59 | guidata(hObject, handles); 60 | 61 | % UIWAIT makes test_listbox_1 wait for user response (see UIRESUME) 62 | % uiwait(handles.figure1); 63 | 64 | 65 | % --- Outputs from this function are returned to the command line. 66 | function varargout = test_listbox_1_OutputFcn(hObject, eventdata, handles) 67 | % varargout cell array for returning output args (see VARARGOUT); 68 | % hObject handle to figure 69 | % eventdata reserved - to be defined in a future version of MATLAB 70 | % handles structure with handles and user data (see GUIDATA) 71 | 72 | % Get default command line output from handles structure 73 | varargout{1} = handles.output; 74 | 75 | 76 | % --- Executes on selection change in Listbox1. 77 | function Listbox1_Callback(hObject, eventdata, handles) 78 | % hObject handle to Listbox1 (see GCBO) 79 | % eventdata reserved - to be defined in a future version of MATLAB 80 | % handles structure with handles and user data (see GUIDATA) 81 | 82 | % If this was a double click, update the label. 83 | selectiontype = get(gcbf,'SelectionType'); 84 | if selectiontype(1) == 'o' 85 | 86 | % Find the value of the popup menu 87 | value = get(handles.Listbox1,'Value'); 88 | 89 | % Update text label 90 | str = ['Option ' num2str(value)]; 91 | set (handles.Label1,'String',str); 92 | end 93 | 94 | 95 | % --- Executes during object creation, after setting all properties. 96 | function Listbox1_CreateFcn(hObject, eventdata, handles) 97 | % hObject handle to Listbox1 (see GCBO) 98 | % eventdata reserved - to be defined in a future version of MATLAB 99 | % handles empty - handles not created until after all CreateFcns called 100 | 101 | % Hint: listbox controls usually have a white background on Windows. 102 | % See ISPC and COMPUTER. 103 | if ispc 104 | set(hObject,'BackgroundColor','white'); 105 | else 106 | set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor')); 107 | end 108 | 109 | 110 | % --- Executes on button press in Button1. 111 | function Button1_Callback(hObject, eventdata, handles) 112 | % hObject handle to Button1 (see GCBO) 113 | % eventdata reserved - to be defined in a future version of MATLAB 114 | % handles structure with handles and user data (see GUIDATA) 115 | 116 | % Find the value of the popup menu 117 | value = get(handles.Listbox1,'Value'); 118 | 119 | % Update text label 120 | str = ['Option ' num2str(value)]; 121 | set (handles.Label1,'String',str); 122 | 123 | 124 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap10/test_menu1.fig: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/101Hub/Matlab101/07273f68f1147a110443aeb121fa10962234f298/assets/《Matlab编程》源码/chap10/test_menu1.fig -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap10/test_popup_menu.fig: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/101Hub/Matlab101/07273f68f1147a110443aeb121fa10962234f298/assets/《Matlab编程》源码/chap10/test_popup_menu.fig -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap10/test_popup_menu.m: -------------------------------------------------------------------------------- 1 | function varargout = test_popup_menu(varargin) 2 | % TEST_POPUP_MENU M-file for test_popup_menu.fig 3 | % TEST_POPUP_MENU, by itself, creates a new TEST_POPUP_MENU or raises the existing 4 | % singleton*. 5 | % 6 | % H = TEST_POPUP_MENU returns the handle to a new TEST_POPUP_MENU or the handle to 7 | % the existing singleton*. 8 | % 9 | % TEST_POPUP_MENU('CALLBACK',hObject,eventData,handles,...) calls the local 10 | % function named CALLBACK in TEST_POPUP_MENU.M with the given input arguments. 11 | % 12 | % TEST_POPUP_MENU('Property','Value',...) creates a new TEST_POPUP_MENU or raises the 13 | % existing singleton*. Starting from the left, property value pairs are 14 | % applied to the GUI before test_popup_menu_OpeningFunction gets called. An 15 | % unrecognized property name or invalid value makes property application 16 | % stop. All inputs are passed to test_popup_menu_OpeningFcn via varargin. 17 | % 18 | % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one 19 | % instance to run (singleton)". 20 | % 21 | % See also: GUIDE, GUIDATA, GUIHANDLES 22 | 23 | % Edit the above text to modify the response to help test_popup_menu 24 | 25 | % Last Modified by GUIDE v2.5 13-Mar-2004 16:54:29 26 | 27 | % Begin initialization code - DO NOT EDIT 28 | gui_Singleton = 1; 29 | gui_State = struct('gui_Name', mfilename, ... 30 | 'gui_Singleton', gui_Singleton, ... 31 | 'gui_OpeningFcn', @test_popup_menu_OpeningFcn, ... 32 | 'gui_OutputFcn', @test_popup_menu_OutputFcn, ... 33 | 'gui_LayoutFcn', [] , ... 34 | 'gui_Callback', []); 35 | if nargin && ischar(varargin{1}) 36 | gui_State.gui_Callback = str2func(varargin{1}); 37 | end 38 | 39 | if nargout 40 | [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); 41 | else 42 | gui_mainfcn(gui_State, varargin{:}); 43 | end 44 | % End initialization code - DO NOT EDIT 45 | 46 | 47 | % --- Executes just before test_popup_menu is made visible. 48 | function test_popup_menu_OpeningFcn(hObject, eventdata, handles, varargin) 49 | % This function has no output args, see OutputFcn. 50 | % hObject handle to figure 51 | % eventdata reserved - to be defined in a future version of MATLAB 52 | % handles structure with handles and user data (see GUIDATA) 53 | % varargin command line arguments to test_popup_menu (see VARARGIN) 54 | 55 | % Choose default command line output for test_popup_menu 56 | handles.output = hObject; 57 | 58 | % Update handles structure 59 | guidata(hObject, handles); 60 | 61 | % UIWAIT makes test_popup_menu wait for user response (see UIRESUME) 62 | % uiwait(handles.figure1); 63 | 64 | 65 | % --- Outputs from this function are returned to the command line. 66 | function varargout = test_popup_menu_OutputFcn(hObject, eventdata, handles) 67 | % varargout cell array for returning output args (see VARARGOUT); 68 | % hObject handle to figure 69 | % eventdata reserved - to be defined in a future version of MATLAB 70 | % handles structure with handles and user data (see GUIDATA) 71 | 72 | % Get default command line output from handles structure 73 | varargout{1} = handles.output; 74 | 75 | 76 | % --- Executes on selection change in Popup1. 77 | function Popup1_Callback(hObject, eventdata, handles) 78 | % hObject handle to Popup1 (see GCBO) 79 | % eventdata reserved - to be defined in a future version of MATLAB 80 | % handles structure with handles and user data (see GUIDATA) 81 | 82 | % Find the value of the popup menu 83 | value = get(handles.Popup1,'Value'); 84 | 85 | % Place the value into the text field 86 | str = ['Option ' num2str(value)]; 87 | set (handles.Label1,'String',str); 88 | 89 | % --- Executes during object creation, after setting all properties. 90 | function Popup1_CreateFcn(hObject, eventdata, handles) 91 | % hObject handle to Popup1 (see GCBO) 92 | % eventdata reserved - to be defined in a future version of MATLAB 93 | % handles empty - handles not created until after all CreateFcns called 94 | 95 | % Hint: popupmenu controls usually have a white background on Windows. 96 | % See ISPC and COMPUTER. 97 | if ispc 98 | set(hObject,'BackgroundColor','white'); 99 | else 100 | set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor')); 101 | end 102 | 103 | 104 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap10/test_radio_button.fig: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/101Hub/Matlab101/07273f68f1147a110443aeb121fa10962234f298/assets/《Matlab编程》源码/chap10/test_radio_button.fig -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap10/test_radio_button.m: -------------------------------------------------------------------------------- 1 | function varargout = test_radio_button(varargin) 2 | % TEST_RADIO_BUTTON M-file for test_radio_button.fig 3 | % TEST_RADIO_BUTTON, by itself, creates a new TEST_RADIO_BUTTON or raises the existing 4 | % singleton*. 5 | % 6 | % H = TEST_RADIO_BUTTON returns the handle to a new TEST_RADIO_BUTTON or the handle to 7 | % the existing singleton*. 8 | % 9 | % TEST_RADIO_BUTTON('CALLBACK',hObject,eventData,handles,...) calls the local 10 | % function named CALLBACK in TEST_RADIO_BUTTON.M with the given input arguments. 11 | % 12 | % TEST_RADIO_BUTTON('Property','Value',...) creates a new TEST_RADIO_BUTTON or raises the 13 | % existing singleton*. Starting from the left, property value pairs are 14 | % applied to the GUI before test_radio_button_OpeningFunction gets called. An 15 | % unrecognized property name or invalid value makes property application 16 | % stop. All inputs are passed to test_radio_button_OpeningFcn via varargin. 17 | % 18 | % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one 19 | % instance to run (singleton)". 20 | % 21 | % See also: GUIDE, GUIDATA, GUIHANDLES 22 | 23 | % Edit the above text to modify the response to help test_radio_button 24 | 25 | % Last Modified by GUIDE v2.5 13-Mar-2004 16:16:51 26 | 27 | % Begin initialization code - DO NOT EDIT 28 | gui_Singleton = 1; 29 | gui_State = struct('gui_Name', mfilename, ... 30 | 'gui_Singleton', gui_Singleton, ... 31 | 'gui_OpeningFcn', @test_radio_button_OpeningFcn, ... 32 | 'gui_OutputFcn', @test_radio_button_OutputFcn, ... 33 | 'gui_LayoutFcn', [] , ... 34 | 'gui_Callback', []); 35 | if nargin && ischar(varargin{1}) 36 | gui_State.gui_Callback = str2func(varargin{1}); 37 | end 38 | 39 | if nargout 40 | [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); 41 | else 42 | gui_mainfcn(gui_State, varargin{:}); 43 | end 44 | % End initialization code - DO NOT EDIT 45 | 46 | 47 | % --- Executes just before test_radio_button is made visible. 48 | function test_radio_button_OpeningFcn(hObject, eventdata, handles, varargin) 49 | % This function has no output args, see OutputFcn. 50 | % hObject handle to figure 51 | % eventdata reserved - to be defined in a future version of MATLAB 52 | % handles structure with handles and user data (see GUIDATA) 53 | % varargin command line arguments to test_radio_button (see VARARGIN) 54 | 55 | % Choose default command line output for test_radio_button 56 | handles.output = hObject; 57 | 58 | % Update handles structure 59 | guidata(hObject, handles); 60 | 61 | % UIWAIT makes test_radio_button wait for user response (see UIRESUME) 62 | % uiwait(handles.figure1); 63 | 64 | 65 | % --- Outputs from this function are returned to the command line. 66 | function varargout = test_radio_button_OutputFcn(hObject, eventdata, handles) 67 | % varargout cell array for returning output args (see VARARGOUT); 68 | % hObject handle to figure 69 | % eventdata reserved - to be defined in a future version of MATLAB 70 | % handles structure with handles and user data (see GUIDATA) 71 | 72 | % Get default command line output from handles structure 73 | varargout{1} = handles.output; 74 | 75 | 76 | % --- Executes on button press in Option1. 77 | function Option1_Callback(hObject, eventdata, handles) 78 | % hObject handle to Option1 (see GCBO) 79 | % eventdata reserved - to be defined in a future version of MATLAB 80 | % handles structure with handles and user data (see GUIDATA) 81 | 82 | % Display the radio button clicked in the text field 83 | set (handles.TextBox,'String','Option 1'); 84 | 85 | % Update all text fields 86 | set (handles.Option1,'Value',1); 87 | set (handles.Option2,'Value',0); 88 | set (handles.Option3,'Value',0); 89 | 90 | % --- Executes on button press in Option2. 91 | function Option2_Callback(hObject, eventdata, handles) 92 | % hObject handle to Option2 (see GCBO) 93 | % eventdata reserved - to be defined in a future version of MATLAB 94 | % handles structure with handles and user data (see GUIDATA) 95 | 96 | % Display the radio button clicked in the text field 97 | set (handles.TextBox,'String','Option 2'); 98 | 99 | % Update all text fields 100 | set (handles.Option1,'Value',0); 101 | set (handles.Option2,'Value',1); 102 | set (handles.Option3,'Value',0); 103 | 104 | % --- Executes on button press in Option3. 105 | function Option3_Callback(hObject, eventdata, handles) 106 | % hObject handle to Option3 (see GCBO) 107 | % eventdata reserved - to be defined in a future version of MATLAB 108 | % handles structure with handles and user data (see GUIDATA) 109 | 110 | % Display the radio button clicked in the text field 111 | set (handles.TextBox,'String','Option 3'); 112 | 113 | % Update all text fields 114 | set (handles.Option1,'Value',0); 115 | set (handles.Option2,'Value',0); 116 | set (handles.Option3,'Value',1); 117 | 118 | 119 | 120 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap10/test_slider.fig: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/101Hub/Matlab101/07273f68f1147a110443aeb121fa10962234f298/assets/《Matlab编程》源码/chap10/test_slider.fig -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap10/test_slider.m: -------------------------------------------------------------------------------- 1 | function varargout = test_slider(varargin) 2 | % TEST_SLIDER M-file for test_slider.fig 3 | % TEST_SLIDER, by itself, creates a new TEST_SLIDER or raises the existing 4 | % singleton*. 5 | % 6 | % H = TEST_SLIDER returns the handle to a new TEST_SLIDER or the handle to 7 | % the existing singleton*. 8 | % 9 | % TEST_SLIDER('CALLBACK',hObject,eventData,handles,...) calls the local 10 | % function named CALLBACK in TEST_SLIDER.M with the given input arguments. 11 | % 12 | % TEST_SLIDER('Property','Value',...) creates a new TEST_SLIDER or raises the 13 | % existing singleton*. Starting from the left, property value pairs are 14 | % applied to the GUI before test_slider_OpeningFunction gets called. An 15 | % unrecognized property name or invalid value makes property application 16 | % stop. All inputs are passed to test_slider_OpeningFcn via varargin. 17 | % 18 | % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one 19 | % instance to run (singleton)". 20 | % 21 | % See also: GUIDE, GUIDATA, GUIHANDLES 22 | 23 | % Edit the above text to modify the response to help test_slider 24 | 25 | % Last Modified by GUIDE v2.5 13-Mar-2004 17:41:39 26 | 27 | % Begin initialization code - DO NOT EDIT 28 | gui_Singleton = 1; 29 | gui_State = struct('gui_Name', mfilename, ... 30 | 'gui_Singleton', gui_Singleton, ... 31 | 'gui_OpeningFcn', @test_slider_OpeningFcn, ... 32 | 'gui_OutputFcn', @test_slider_OutputFcn, ... 33 | 'gui_LayoutFcn', [] , ... 34 | 'gui_Callback', []); 35 | if nargin && ischar(varargin{1}) 36 | gui_State.gui_Callback = str2func(varargin{1}); 37 | end 38 | 39 | if nargout 40 | [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); 41 | else 42 | gui_mainfcn(gui_State, varargin{:}); 43 | end 44 | % End initialization code - DO NOT EDIT 45 | 46 | 47 | % --- Executes just before test_slider is made visible. 48 | function test_slider_OpeningFcn(hObject, eventdata, handles, varargin) 49 | % This function has no output args, see OutputFcn. 50 | % hObject handle to figure 51 | % eventdata reserved - to be defined in a future version of MATLAB 52 | % handles structure with handles and user data (see GUIDATA) 53 | % varargin command line arguments to test_slider (see VARARGIN) 54 | 55 | % Choose default command line output for test_slider 56 | handles.output = hObject; 57 | 58 | % Update handles structure 59 | guidata(hObject, handles); 60 | 61 | % UIWAIT makes test_slider wait for user response (see UIRESUME) 62 | % uiwait(handles.figure1); 63 | 64 | 65 | % --- Outputs from this function are returned to the command line. 66 | function varargout = test_slider_OutputFcn(hObject, eventdata, handles) 67 | % varargout cell array for returning output args (see VARARGOUT); 68 | % hObject handle to figure 69 | % eventdata reserved - to be defined in a future version of MATLAB 70 | % handles structure with handles and user data (see GUIDATA) 71 | 72 | % Get default command line output from handles structure 73 | varargout{1} = handles.output; 74 | 75 | 76 | % --- Executes on slider movement. 77 | function Slider1_Callback(hObject, eventdata, handles) 78 | % hObject handle to Slider1 (see GCBO) 79 | % eventdata reserved - to be defined in a future version of MATLAB 80 | % handles structure with handles and user data (see GUIDATA) 81 | 82 | % Find the value of the slider 83 | value = get(handles.Slider1,'Value'); 84 | 85 | % Place the value in the text field 86 | str = sprintf('%.2f',value); 87 | set (handles.Label1,'String',str); 88 | 89 | 90 | % --- Executes during object creation, after setting all properties. 91 | function Slider1_CreateFcn(hObject, eventdata, handles) 92 | % hObject handle to Slider1 (see GCBO) 93 | % eventdata reserved - to be defined in a future version of MATLAB 94 | % handles empty - handles not created until after all CreateFcns called 95 | 96 | % Hint: slider controls usually have a light gray background, change 97 | % 'usewhitebg' to 0 to use default. See ISPC and COMPUTER. 98 | usewhitebg = 1; 99 | if usewhitebg 100 | set(hObject,'BackgroundColor',[.9 .9 .9]); 101 | else 102 | set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor')); 103 | end 104 | 105 | 106 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap10/test_textbox.fig: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/101Hub/Matlab101/07273f68f1147a110443aeb121fa10962234f298/assets/《Matlab编程》源码/chap10/test_textbox.fig -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap10/test_textbox.m: -------------------------------------------------------------------------------- 1 | function varargout = test_textbox(varargin) 2 | % TEST_TEXTBOX M-file for test_textbox.fig 3 | % TEST_TEXTBOX, by itself, creates a new TEST_TEXTBOX or raises the existing 4 | % singleton*. 5 | % 6 | % H = TEST_TEXTBOX returns the handle to a new TEST_TEXTBOX or the handle to 7 | % the existing singleton*. 8 | % 9 | % TEST_TEXTBOX('CALLBACK',hObject,eventData,handles,...) calls the local 10 | % function named CALLBACK in TEST_TEXTBOX.M with the given input arguments. 11 | % 12 | % TEST_TEXTBOX('Property','Value',...) creates a new TEST_TEXTBOX or raises the 13 | % existing singleton*. Starting from the left, property value pairs are 14 | % applied to the GUI before test_textbox_OpeningFunction gets called. An 15 | % unrecognized property name or invalid value makes property application 16 | % stop. All inputs are passed to test_textbox_OpeningFcn via varargin. 17 | % 18 | % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one 19 | % instance to run (singleton)". 20 | % 21 | % See also: GUIDE, GUIDATA, GUIHANDLES 22 | 23 | % Edit the above text to modify the response to help test_textbox 24 | 25 | % Last Modified by GUIDE v2.5 25-Mar-2002 12:19:15 26 | 27 | % Begin initialization code - DO NOT EDIT 28 | gui_Singleton = 1; 29 | gui_State = struct('gui_Name', mfilename, ... 30 | 'gui_Singleton', gui_Singleton, ... 31 | 'gui_OpeningFcn', @test_textbox_OpeningFcn, ... 32 | 'gui_OutputFcn', @test_textbox_OutputFcn, ... 33 | 'gui_LayoutFcn', [] , ... 34 | 'gui_Callback', []); 35 | if nargin && ischar(varargin{1}) 36 | gui_State.gui_Callback = str2func(varargin{1}); 37 | end 38 | 39 | if nargout 40 | [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); 41 | else 42 | gui_mainfcn(gui_State, varargin{:}); 43 | end 44 | % End initialization code - DO NOT EDIT 45 | 46 | 47 | % --- Executes just before test_textbox is made visible. 48 | function test_textbox_OpeningFcn(hObject, eventdata, handles, varargin) 49 | % This function has no output args, see OutputFcn. 50 | % hObject handle to figure 51 | % eventdata reserved - to be defined in a future version of MATLAB 52 | % handles structure with handles and user data (see GUIDATA) 53 | % varargin command line arguments to test_textbox (see VARARGIN) 54 | 55 | % Choose default command line output for test_textbox 56 | handles.output = hObject; 57 | 58 | % Update handles structure 59 | guidata(hObject, handles); 60 | 61 | % UIWAIT makes test_textbox wait for user response (see UIRESUME) 62 | % uiwait(handles.figure1); 63 | 64 | 65 | % --- Outputs from this function are returned to the command line. 66 | function varargout = test_textbox_OutputFcn(hObject, eventdata, handles) 67 | % varargout cell array for returning output args (see VARARGOUT); 68 | % hObject handle to figure 69 | % eventdata reserved - to be defined in a future version of MATLAB 70 | % handles structure with handles and user data (see GUIDATA) 71 | 72 | % Get default command line output from handles structure 73 | varargout{1} = handles.output; 74 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap10/test_toggle.fig: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/101Hub/Matlab101/07273f68f1147a110443aeb121fa10962234f298/assets/《Matlab编程》源码/chap10/test_toggle.fig -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap10/test_toggle.m: -------------------------------------------------------------------------------- 1 | function varargout = test_toggle(varargin) 2 | % TEST_TOGGLE M-file for test_toggle.fig 3 | % TEST_TOGGLE, by itself, creates a new TEST_TOGGLE or raises the existing 4 | % singleton*. 5 | % 6 | % H = TEST_TOGGLE returns the handle to a new TEST_TOGGLE or the handle to 7 | % the existing singleton*. 8 | % 9 | % TEST_TOGGLE('CALLBACK',hObject,eventData,handles,...) calls the local 10 | % function named CALLBACK in TEST_TOGGLE.M with the given input arguments. 11 | % 12 | % TEST_TOGGLE('Property','Value',...) creates a new TEST_TOGGLE or raises the 13 | % existing singleton*. Starting from the left, property value pairs are 14 | % applied to the GUI before test_toggle_OpeningFunction gets called. An 15 | % unrecognized property name or invalid value makes property application 16 | % stop. All inputs are passed to test_toggle_OpeningFcn via varargin. 17 | % 18 | % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one 19 | % instance to run (singleton)". 20 | % 21 | % See also: GUIDE, GUIDATA, GUIHANDLES 22 | 23 | % Edit the above text to modify the response to help test_toggle 24 | 25 | % Last Modified by GUIDE v2.5 13-Mar-2004 15:41:03 26 | 27 | % Begin initialization code - DO NOT EDIT 28 | gui_Singleton = 1; 29 | gui_State = struct('gui_Name', mfilename, ... 30 | 'gui_Singleton', gui_Singleton, ... 31 | 'gui_OpeningFcn', @test_toggle_OpeningFcn, ... 32 | 'gui_OutputFcn', @test_toggle_OutputFcn, ... 33 | 'gui_LayoutFcn', [] , ... 34 | 'gui_Callback', []); 35 | if nargin && ischar(varargin{1}) 36 | gui_State.gui_Callback = str2func(varargin{1}); 37 | end 38 | 39 | if nargout 40 | [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); 41 | else 42 | gui_mainfcn(gui_State, varargin{:}); 43 | end 44 | % End initialization code - DO NOT EDIT 45 | 46 | 47 | % --- Executes just before test_toggle is made visible. 48 | function test_toggle_OpeningFcn(hObject, eventdata, handles, varargin) 49 | % This function has no output args, see OutputFcn. 50 | % hObject handle to figure 51 | % eventdata reserved - to be defined in a future version of MATLAB 52 | % handles structure with handles and user data (see GUIDATA) 53 | % varargin command line arguments to test_toggle (see VARARGIN) 54 | 55 | % Choose default command line output for test_toggle 56 | handles.output = hObject; 57 | 58 | % Update handles structure 59 | guidata(hObject, handles); 60 | 61 | % UIWAIT makes test_toggle wait for user response (see UIRESUME) 62 | % uiwait(handles.figure1); 63 | 64 | 65 | % --- Outputs from this function are returned to the command line. 66 | function varargout = test_toggle_OutputFcn(hObject, eventdata, handles) 67 | % varargout cell array for returning output args (see VARARGOUT); 68 | % hObject handle to figure 69 | % eventdata reserved - to be defined in a future version of MATLAB 70 | % handles structure with handles and user data (see GUIDATA) 71 | 72 | % Get default command line output from handles structure 73 | varargout{1} = handles.output; 74 | 75 | 76 | % --- Executes on button press in ToggleButton. 77 | function ToggleButton_Callback(hObject, eventdata, handles) 78 | % hObject handle to ToggleButton (see GCBO) 79 | % eventdata reserved - to be defined in a future version of MATLAB 80 | % handles structure with handles and user data (see GUIDATA) 81 | 82 | % Find the state of the toggle button 83 | state = get(handles.ToggleButton,'Value'); 84 | 85 | % Place the value into the text field 86 | if state == 0 87 | set (handles.TextBox,'String','Off'); 88 | else 89 | set (handles.TextBox,'String','On'); 90 | end 91 | 92 | 93 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap2/c14_date.m: -------------------------------------------------------------------------------- 1 | % Script file: c14_date.m 2 | % 3 | % Purpose: 4 | % To calculate the age of an organic sample from the percentage 5 | % of the original carbon 14 remaining in the sample. 6 | % 7 | % Record of revisions: 8 | % Date Programmer Description of change 9 | % ==== ========== ===================== 10 | % 01/05/07 S. J. Chapman Original code 11 | % 12 | % Define variables: 13 | % age -- The age of the sample in years 14 | % lamda -- The radioactive decay constant for carbon-14, 15 | % in units of 1/years. 16 | % percent -- The percentage of carbon 14 remaining at the time 17 | % of the measurement 18 | % ratio -- The ratio of the carbon 14 remaining at the time 19 | % of the measurement to the original amount of 20 | % carbon 14. 21 | 22 | % Set decay constant for carbon-14 23 | lamda = 0.00012097; 24 | 25 | % Prompt the user for the percentage of C-14 remaining. 26 | percent = input('Enter the percentage of carbon 14 remaining:\n'); 27 | 28 | % Perform calculations 29 | ratio = percent / 100; % Convert to fractional ratio 30 | age = (-1.0 / lamda) * log(ratio); % Get age in years 31 | 32 | % Tell the user about the age of the sample. 33 | string = ['The age of the sample is ' num2str(age) ' years.']; 34 | disp(string); 35 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap2/calc_area.m: -------------------------------------------------------------------------------- 1 | % This m-file calculates the area of a circle, 2 | % and displays the result. 3 | radius = 2.5; 4 | area = pi * 2.5^2; 5 | string = ['The area of the circle is ' num2str(area)]; 6 | disp(string); 7 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap2/calc_power.m: -------------------------------------------------------------------------------- 1 | % Script file: calc_power.m 2 | % 3 | % Purpose: 4 | % To calculate and plot the power supplied to a load as 5 | % as a function of the load resistance. 6 | % 7 | % Record of revisions: 8 | % Date Programmer Description of change 9 | % ==== ========== ===================== 10 | % 01/03/07 S. J. Chapman Original code 11 | % 12 | % Define variables: 13 | % amps -- Current flow to load (amps) 14 | % pl -- Power supplied to load (watts) 15 | % rl -- Resistance of the load (ohms) 16 | % rs -- Internal resistance of the power source (ohms) 17 | % volts -- Voltage of the power source (volts) 18 | 19 | % Set the values of source voltage and internal resistance 20 | volts = 120; 21 | rs = 50; 22 | 23 | % Create an array of load resistances 24 | rl = 1:1:100; 25 | 26 | % Calculate the current flow for each resistance 27 | amps = volts ./ ( rs + rl ); 28 | 29 | % Calculate the power supplied to the load 30 | pl = (amps .^ 2) .* rl; 31 | 32 | % Plot the power versus load resistance 33 | plot(rl,pl); 34 | title('Plot of power versus load resistance'); 35 | xlabel('Load resistance (ohms)'); 36 | ylabel('Power (watts)'); 37 | grid on; 38 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap2/fig2_12.m: -------------------------------------------------------------------------------- 1 | x = 0:0.1:10; 2 | y = x.^2 - 10.*x + 26; 3 | figure(1) 4 | subplot(2,2,1) 5 | plot(x,y); 6 | title ('Linear Plot'); 7 | xlabel ('x'); 8 | ylabel ('y'); 9 | grid on; 10 | subplot(2,2,2) 11 | semilogx(x,y); 12 | title ('Semilog x Plot'); 13 | xlabel ('x'); 14 | ylabel ('y'); 15 | grid on; 16 | subplot(2,2,3) 17 | semilogy(x,y); 18 | title ('Semilog y Plot'); 19 | xlabel ('x'); 20 | ylabel ('y'); 21 | grid on; 22 | subplot(2,2,4) 23 | loglog(x,y); 24 | title ('Loglog Plot'); 25 | xlabel ('x'); 26 | ylabel ('y'); 27 | grid on; 28 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap2/fprintf_example.m: -------------------------------------------------------------------------------- 1 | x = 2 * ( 1 - 2*i )^3; 2 | str = ['disp: x = ' num2str(x)]; 3 | disp(str); 4 | fprintf('fprintf: x = %8.4f\n',x); -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap2/quiz_area.m: -------------------------------------------------------------------------------- 1 | radius = input('Enter circle radius:\n'); 2 | area = pi * radius^2; 3 | str = ['The area is ' num2str(area)]; 4 | disp(str); 5 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap2/sin_x.m: -------------------------------------------------------------------------------- 1 | % sin_x.m: This m-file calculates and plots the 2 | % function sin(x) for 0 <= x <= 6. 3 | x = 0:0.1:6; 4 | y = sin(x); 5 | plot(x,y); 6 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap2/temp_conversion.m: -------------------------------------------------------------------------------- 1 | % Script file: temp_conversion.m 2 | % 3 | % Purpose: 4 | % To convert an input temperature from degrees Fahrenheit to 5 | % an output temperature in kelvin. 6 | % 7 | % Record of revisions: 8 | % Date Programmer Description of change 9 | % ==== ========== ===================== 10 | % 01/03/07 S. J. Chapman Original code 11 | % 12 | % Define variables: 13 | % temp_f -- Temperature in degrees Fahrenheit 14 | % temp_k -- Temperature in kelvin 15 | 16 | % Prompt the user for the input temperature. 17 | temp_f = input('Enter the temperature in degrees Fahrenheit: '); 18 | 19 | % Convert to kelvin. 20 | temp_k = (5/9) * (temp_f - 32) + 273.15; 21 | 22 | % Write out the result. 23 | fprintf('%6.2f degrees Fahrenheit = %6.2f kelvin.\n', ... 24 | temp_f,temp_k); 25 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap2/test.m: -------------------------------------------------------------------------------- 1 | y = 17; 2 | x = (y + 3) / 2); -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap2/xxx.dat: -------------------------------------------------------------------------------- 1 | 10, 2 2 | 3, 5 -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap3/calc_roots.m: -------------------------------------------------------------------------------- 1 | % Script file: calc_roots.m 2 | % 3 | % Purpose: 4 | % This program solves for the roots of a quadratic equation 5 | % of the form a*x**2 + b*x + c = 0. It calculates the answers 6 | % regardless of the type of roots that the equation possesses. 7 | % 8 | % Record of revisions: 9 | % Date Programmer Description of change 10 | % ==== ========== ===================== 11 | % 01/12/07 S. J. Chapman Original code 12 | % 13 | % Define variables: 14 | % a -- Coefficient of x^2 term of equation 15 | % b -- Coefficient of x term of equation 16 | % c -- Constant term of equation 17 | % discriminant -- Discriminant of the equation 18 | % imag_part -- Imag part of equation (for complex roots) 19 | % real_part -- Real part of equation (for complex roots) 20 | % x1 -- First solution of equation (for real roots) 21 | % x2 -- Second solution of equation (for real roots) 22 | 23 | % Prompt the user for the coefficients of the equation 24 | disp ('This program solves for the roots of a quadratic '); 25 | disp ('equation of the form A*X^2 + B*X + C = 0. '); 26 | a = input ('Enter the coefficient A: '); 27 | b = input ('Enter the coefficient B: '); 28 | c = input ('Enter the coefficient C: '); 29 | 30 | % Calculate discriminant 31 | discriminant = b^2 - 4 * a * c; 32 | 33 | % Solve for the roots, depending on the value of the discriminant 34 | if discriminant > 0 % there are two real roots, so... 35 | 36 | x1 = ( -b + sqrt(discriminant) ) / ( 2 * a ); 37 | x2 = ( -b - sqrt(discriminant) ) / ( 2 * a ); 38 | disp ('This equation has two real roots:'); 39 | fprintf ('x1 = %f\n', x1); 40 | fprintf ('x2 = %f\n', x2); 41 | 42 | elseif discriminant == 0 % there is one repeated root, so... 43 | 44 | x1 = ( -b ) / ( 2 * a ); 45 | disp ('This equation has two identical real roots:'); 46 | fprintf ('x1 = x2 = %f\n', x1); 47 | 48 | else % there are complex roots, so ... 49 | 50 | real_part = ( -b ) / ( 2 * a ); 51 | imag_part = sqrt ( abs ( discriminant ) ) / ( 2 * a ); 52 | disp ('This equation has complex roots:'); 53 | fprintf('x1 = %f +i %f\n', real_part, imag_part ); 54 | fprintf('x1 = %f -i %f\n', real_part, imag_part ); 55 | 56 | end 57 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap3/fig3-13.fig: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/101Hub/Matlab101/07273f68f1147a110443aeb121fa10962234f298/assets/《Matlab编程》源码/chap3/fig3-13.fig -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap3/fig3-15.fig: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/101Hub/Matlab101/07273f68f1147a110443aeb121fa10962234f298/assets/《Matlab编程》源码/chap3/fig3-15.fig -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap3/funxy.m: -------------------------------------------------------------------------------- 1 | % Script file: funxy.m 2 | % 3 | % Purpose: 4 | % This program solves the function f(x,y) for a 5 | % user-specified x and y, where f(x,y) is defined as: 6 | % _ 7 | % | 8 | % | x + y x >= 0 and y >= 0 9 | % | x + y^2 x >= 0 and y < 0 10 | % f(x,y) = | x^2 + y x < 0 and y >= 0 11 | % | x^2 + y^2 x < 0 and y < 0 12 | % |_ 13 | % 14 | % Record of revisions: 15 | % Date Programmer Description of change 16 | % ==== ========== ===================== 17 | % 01/13/07 S. J. Chapman Original code 18 | % 19 | % Define variables: 20 | % x -- First independent variable 21 | % y -- Second independent variable 22 | % fun -- Resulting function 23 | 24 | % Prompt the user for the values x and y 25 | x = input ('Enter the x coefficient: '); 26 | y = input ('Enter the y coefficient: '); 27 | 28 | % Calculate the function f(x,y) based upon 29 | % the signs of x and y. 30 | if x >= 0 && y >= 0 31 | fun = x + y; 32 | elseif x >= 0 && y < 0 33 | fun = x + y^2; 34 | elseif x < 0 && y >= 0 35 | fun = x^2 + y; 36 | else 37 | fun = x^2 + y^2; 38 | end 39 | 40 | % Write the value of the function. 41 | disp (['The value of the function is ' num2str(fun)]); 42 | 43 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap3/ideal_gas.m: -------------------------------------------------------------------------------- 1 | % Script file: ideal_gas.m 2 | % 3 | % Purpose: 4 | % This program plots the pressure versus volume of an 5 | % ideal gas. 6 | % 7 | % Record of revisions: 8 | % Date Programmer Description of change 9 | % ==== ========== ===================== 10 | % 01/16/07 S. J. Chapman Original code 11 | % 12 | % Define variables: 13 | % n -- Number of atoms (mol) 14 | % P -- Pressure (kPa) 15 | % R -- Ideal gas constant (L kPa/mol K) 16 | % T -- Temperature (K) 17 | % V -- volume (L) 18 | 19 | % Initialize nRT 20 | n = 1; % Moles of atoms 21 | R = 8.314; % Ideal gas constant 22 | T = 273; % Temperature (K) 23 | 24 | % Create array of input pressures. Note that this 25 | % array must be quite dense to catch the major 26 | % changes in volume at low pressures. 27 | P = 1:0.1:1000; 28 | 29 | % Calculate volumes 30 | V = (n * R * T) ./ P; 31 | 32 | % Create first plot 33 | figure(1); 34 | loglog( P, V, 'r-', 'LineWidth', 2 ); 35 | title('\bfVolume vs Pressure in an Ideal Gas'); 36 | xlabel('\bfPressure (kPa)'); 37 | ylabel('\bfVolume (L)'); 38 | grid on; 39 | hold on; 40 | 41 | % Now increase temperature 42 | T = 373; % Temperature (K) 43 | 44 | % Calculate volumes 45 | V = (n * R * T) ./ P; 46 | 47 | % Add second line to plot 48 | figure(1); 49 | loglog( P, V, 'b--', 'LineWidth', 2 ); 50 | hold off; 51 | 52 | % Add legend 53 | legend('T = 273 K','T = 373 K'); 54 | 55 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap3/junk.m: -------------------------------------------------------------------------------- 1 | x = 5; 2 | y = -2 3 | 4 | if x >= 0 & y >= 0 5 | fun = x + y; 6 | elseif x >= 0 & y < 0 7 | fun = x + y^2; 8 | elseif x < 0 & y >= 0 9 | fun = x^2 + y; 10 | else 11 | fun = x^2 + y^2; 12 | end 13 | 14 | disp (['The value of the function is ' num2str(fun)]); 15 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap3/microphone.m: -------------------------------------------------------------------------------- 1 | % Script file: microphone.m 2 | % 3 | % Purpose: 4 | % This program plots the gain pattern of a cardioid 5 | % microphone. 6 | % 7 | % Record of revisions: 8 | % Date Programmer Description of change 9 | % ==== ========== ===================== 10 | % 01/15/07 S. J. Chapman Original code 11 | % 12 | % Define variables: 13 | % g -- Microphone gain constant 14 | % gain -- Gain as a function of angle 15 | % theta -- Angle from microphone axis (radians) 16 | 17 | % Calculate gain versus angle 18 | g = 0.5; 19 | theta = 0:pi/20:2*pi; 20 | gain = 2*g*(1+cos(theta)); 21 | 22 | % Plot gain 23 | polar (theta,gain,'r-'); 24 | title ('\bfGain versus angle \theta'); 25 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap3/plot_filter.m: -------------------------------------------------------------------------------- 1 | % Script file: plot_filter.m 2 | % 3 | % Purpose: 4 | % This program plots the amplitude and phase responses 5 | % of a low-padd RC filter. 6 | % 7 | % Record of revisions: 8 | % Date Programmer Description of change 9 | % ==== ========== ===================== 10 | % 01/15/07 S. J. Chapman Original code 11 | % 12 | % Define variables: 13 | % amp -- Amplitude response 14 | % C -- Capacitiance (farads) 15 | % f -- Frequency of input signal (Hz) 16 | % phase -- Phase response 17 | % R -- Resistance (ohms) 18 | % res -- Vo/Vi 19 | 20 | % Initialize R & C 21 | R = 16000; % 16 k ohms 22 | C = 1.0E-6; % 1 uF 23 | 24 | % Create array of input frequencies 25 | f = 1:2:1000; 26 | 27 | % Calculate response 28 | res = 1 ./ ( 1 + j*2*pi*f*R*C ); 29 | 30 | % Calculate amplitude response 31 | amp = abs(res); 32 | 33 | % Calculate phase response 34 | phase = angle(res); 35 | 36 | % Create plots 37 | subplot(2,1,1); 38 | loglog( f, amp ); 39 | title('Amplitude Response'); 40 | xlabel('Frequency (Hz)'); 41 | ylabel('Output/Input Ratio'); 42 | grid on; 43 | 44 | subplot(2,1,2); 45 | semilogx( f, phase ); 46 | title('Phase Response'); 47 | xlabel('Frequency (Hz)'); 48 | ylabel('Output-Input Phase (rad)'); 49 | grid on; 50 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap4/ball.m: -------------------------------------------------------------------------------- 1 | % Script file: ball.m 2 | % 3 | % Purpose: 4 | % This program calculates the distance traveled by a ball 5 | % thrown at a specified angle "theta" and a specified 6 | % velocity "vo" from a point on the surface of the Earth, 7 | % ignoring air friction and the Earth's curvature. It 8 | % calculates the angle yielding maximum range, and also 9 | % plots selected trajectories. 10 | % 11 | % Record of revisions: 12 | % Date Programmer Description of change 13 | % ==== ========== ===================== 14 | % 01/30/07 S. J. Chapman Original code 15 | % 16 | % Define variables: 17 | % conv -- Degrees to radians conv factor 18 | % gravity -- Accel. due to gravity (m/s^2) 19 | % ii, jj -- Loop index 20 | % index -- Location of maximum range in array 21 | % maxangle -- Angle that gives maximum range (deg) 22 | % maxrange -- Maximum range (m) 23 | % range -- Range for a particular angle (m) 24 | % time -- Time (s) 25 | % theta -- Initial angle (deg) 26 | % traj_time -- Total trajectory time (s) 27 | % vo -- Initial velocity (m/s) 28 | % vxo -- X-component of initial velocity (m/s) 29 | % vyo -- Y-component of initial velocity (m/s) 30 | % x -- X-position of ball (m) 31 | % y -- Y-position of ball (m) 32 | 33 | % Constants 34 | conv = pi / 180; % Degrees-to-radians conversion factor 35 | g = -9.81; % Accel. due to gravity 36 | vo = 20; % Initial velocity 37 | 38 | %Create an array to hold ranges 39 | range = zeros(1,91); 40 | 41 | % Calculate maximum ranges 42 | for ii = 1:91 43 | theta = ii - 1; 44 | vxo = vo * cos(theta*conv); 45 | vyo = vo * sin(theta*conv); 46 | max_time = -2 * vyo / g; 47 | range(ii) = vxo * max_time; 48 | end 49 | 50 | % Write out table of ranges 51 | fprintf ('Range versus angle theta:\n'); 52 | for ii = 1:91 53 | theta = ii - 1; 54 | fprintf(' %2d %8.4f\n',theta, range(ii)); 55 | end 56 | 57 | % Calculate the maximum range and angle 58 | [maxrange index] = max(range); 59 | maxangle = index - 1; 60 | fprintf ('\nMax range is %8.4f at %2d degrees.\n',... 61 | maxrange, maxangle); 62 | 63 | % Now plot the trajectories 64 | for ii = 5:10:85 65 | 66 | % Get velocities and max time for this angle 67 | theta = ii; 68 | vxo = vo * cos(theta*conv); 69 | vyo = vo * sin(theta*conv); 70 | max_time = -2 * vyo / g; 71 | 72 | % Calculate the (x,y) positions 73 | x = zeros(1,21); 74 | y = zeros(1,21); 75 | for jj = 1:21 76 | time = (jj-1) * max_time/20; 77 | x(jj) = vxo * time; 78 | y(jj) = vyo * time + 0.5 * g * time^2; 79 | end 80 | plot(x,y,'b'); 81 | if ii == 5 82 | hold on; 83 | end 84 | end 85 | 86 | % Add titles and axis lables 87 | title ('\bfTrajectory of Ball vs Initial Angle \theta'); 88 | xlabel ('\bf\itx \rm\bf(meters)'); 89 | ylabel ('\bf\ity \rm\bf(meters)'); 90 | axis ([0 45 0 25]); 91 | grid on; 92 | 93 | % Now plot the max range trajectory 94 | vxo = vo * cos(maxangle*conv); 95 | vyo = vo * sin(maxangle*conv); 96 | max_time = -2 * vyo / g; 97 | 98 | % Calculate the (x,y) positions 99 | x = zeros(1,21); 100 | y = zeros(1,21); 101 | for jj = 1:21 102 | time = (jj-1) * max_time/20; 103 | x(jj) = vxo * time; 104 | y(jj) = vyo * time + 0.5 * g * time^2; 105 | end 106 | plot(x,y,'r','LineWidth',3.0); 107 | hold off 108 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap4/ball_2/ball.m: -------------------------------------------------------------------------------- 1 | % Script file: ball.m 2 | % 3 | % Purpose: 4 | % This program calculates the distance traveled by a ball 5 | % thrown at a specified angle "theta" and a specified 6 | % velocity "vo" from a point on the surface of the Earth, 7 | % ignoring air friction and the Earth's curvature. It 8 | % calculates the angle yielding maximum range, and also 9 | % plots selected trajectories. 10 | % 11 | % Record of revisions: 12 | % Date Programmer Description of change 13 | % ==== ========== ===================== 14 | % 01/30/07 S. J. Chapman Original code 15 | % 1. 02/07/07 S. J. Chapman Vectorized inner loops 16 | % 17 | % Define variables: 18 | % conv -- Degrees to radians conv factor 19 | % gravity -- Accel. due to gravity (m/s^2) 20 | % ii, jj -- Loop index 21 | % index -- Location of maximum range in array 22 | % maxangle -- Angle that gives maximum range (deg) 23 | % maxrange -- Maximum range (m) 24 | % range -- Range for a particular angle (m) 25 | % time -- Time (s) 26 | % theta -- Initial angle (deg) 27 | % traj_time -- Total trajectory time (s) 28 | % vo -- Initial velocity (m/s) 29 | % vxo -- X-component of initial velocity (m/s) 30 | % vyo -- Y-component of initial velocity (m/s) 31 | % x -- X-position of ball (m) 32 | % y -- Y-position of ball (m) 33 | 34 | % Constants 35 | conv = pi / 180; % Degrees-to-radians conversion factor 36 | g = -9.81; % Accel. due to gravity 37 | vo = 20; % Initial velocity 38 | 39 | %Create an array to hold ranges 40 | range = zeros(1,91); 41 | 42 | % Calculate maximum ranges (vectorized) 43 | theta = 0:90; 44 | vxo = vo * cos(theta*conv); 45 | vyo = vo * sin(theta*conv); 46 | max_time = -2 * vyo / g; 47 | range = vxo .* max_time; 48 | 49 | % Write out table of ranges 50 | fprintf ('Range versus angle theta:\n'); 51 | for ii = 1:91 52 | theta = ii - 1; 53 | fprintf(' %2d %8.4f\n',theta, range(ii)); 54 | end 55 | 56 | % Calculate the maximum range and angle 57 | [maxrange index] = max(range); 58 | maxangle = index - 1; 59 | fprintf ('\nMax range is %8.4f at %2d degrees.\n',... 60 | maxrange, maxangle); 61 | 62 | % Now plot the trajectories 63 | for ii = 5:10:85 64 | 65 | % Get velocities and max time for this angle 66 | theta = ii; 67 | vxo = vo * cos(theta*conv); 68 | vyo = vo * sin(theta*conv); 69 | traj_time = -2 * vyo / g; 70 | 71 | % Calculate the (x,y) positions (vectorized) 72 | time = 0:0.05:1 * max_time; 73 | x = vxo * time; 74 | y = vyo * time + 0.5 * g * time.^2; 75 | plot(x,y,'b'); 76 | if ii == 5 77 | hold on; 78 | end 79 | end 80 | 81 | % Add titles and axis lables 82 | title ('\bfTrajectory of Ball vs Initial Angle \theta'); 83 | xlabel ('\bf\itx \rm\bf(meters)'); 84 | ylabel ('\bf\ity \rm\bf(meters)'); 85 | axis ([0 45 0 25]); 86 | grid on; 87 | 88 | % Now plot the max range trajectory 89 | vxo = vo * cos(maxangle*conv); 90 | vyo = vo * sin(maxangle*conv); 91 | traj_time = -2 * vyo / g; 92 | 93 | % Calculate the (x,y) positions (vectorized) 94 | time = 0:0.05:1 * max_time; 95 | x = vxo * time; 96 | y = vyo * time + 0.5 * g * time.^2; 97 | plot(x,y,'r','LineWidth',3.0); 98 | hold off 99 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap4/doy.m: -------------------------------------------------------------------------------- 1 | % Script file: doy.m 2 | % 3 | % Purpose: 4 | % This program calculates the day of year corresponding 5 | % to a specified date. It illustrates the use switch 6 | % and for constructs. 7 | % 8 | % Record of revisions: 9 | % Date Programmer Description of change 10 | % ==== ========== ===================== 11 | % 01/27/07 S. J. Chapman Original code 12 | % 13 | % Define variables: 14 | % day -- Day (dd) 15 | % day_of_year -- Day of year 16 | % ii -- Loop index 17 | % leap_day -- Extra day for leap year 18 | % month -- Month (mm) 19 | % year -- Year (yyyy) 20 | 21 | % Get day, month, and year to convert 22 | disp('This program calculates the day of year given the '); 23 | disp('specified date.'); 24 | month = input('Enter specified month (1-12): '); 25 | day = input('Enter specified day(1-31): '); 26 | year = input('Enter specified year(yyyy): '); 27 | 28 | % Check for leap year, and add extra day if necessary 29 | if mod(year,400) == 0 30 | leap_day = 1; % Years divisible by 400 are leap years 31 | elseif mod(year,100) == 0 32 | leap_day = 0; % Other centuries are not leap years 33 | elseif mod(year,4) == 0 34 | leap_day = 1; % Otherwise every 4th year is a leap year 35 | else 36 | leap_day = 0; % Other years are not leap years 37 | end 38 | 39 | % Calculate day of year by adding current day to the 40 | % days in previous months. 41 | day_of_year = day; 42 | for ii = 1:month-1 43 | 44 | % Add days in months from January to last month 45 | switch (ii) 46 | case {1,3,5,7,8,10,12}, 47 | day_of_year = day_of_year + 31; 48 | case {4,6,9,11}, 49 | day_of_year = day_of_year + 30; 50 | case 2, 51 | day_of_year = day_of_year + 28 + leap_day; 52 | end 53 | 54 | end 55 | 56 | % Tell user 57 | fprintf('The date %2d/%2d/%4d is day of year %d.\n', ... 58 | month, day, year, day_of_year); 59 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap4/logical1.m: -------------------------------------------------------------------------------- 1 | % Script file: logical1.m 2 | % 3 | % Purpose: 4 | % This program calculates the time required to 5 | % calculate the square roots of all elements in 6 | % array a whose value exceeds 5000. This is done 7 | % in two different ways: 8 | % 1. Using a for loop and if construct. 9 | % 2. Using a logical array. 10 | % 11 | % Record of revisions: 12 | % Date Programmer Description of change 13 | % ==== ========== ===================== 14 | % 01/27/07 S. J. Chapman Original code 15 | % 16 | % Define variables: 17 | % a -- Array of input values 18 | % b -- Logical array to serve as a mask 19 | % ii, jj -- Loop index 20 | % average1 -- Average time for calculation 1 21 | % average2 -- Average time for calculation 2 22 | % maxcount -- Number of times to loop calculation 23 | % month -- Month (mm) 24 | % year -- Year (yyyy) 25 | 26 | 27 | % Perform calculation using loops and branches. 28 | maxcount = 10; % Number of repetitions 29 | tic; % Start timer 30 | for jj = 1:maxcount 31 | a = 1:10000; % Declare array a 32 | for ii = 1:10000 33 | if a(ii) > 5000 34 | a(ii) = sqrt(a(ii)); 35 | end 36 | end 37 | end 38 | average1 = (toc)/maxcount; % Calculate average time 39 | 40 | 41 | % Perform calculation using logical arrays. 42 | maxcount = 100; % Number of repetitions 43 | tic; % Start timer 44 | for jj = 1:maxcount 45 | a = 1:10000; % Declare array a 46 | b = a > 5000; % Create mask 47 | a(b) = sqrt(a(b)); % Take square root 48 | end 49 | average2 = (toc)/maxcount; % Calculate average time 50 | 51 | % Display results 52 | fprintf('Loop / if approach = %8.4f\n', average1); 53 | fprintf('Logical array approach = %8.4f\n', average2); 54 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap4/lsqfit.m: -------------------------------------------------------------------------------- 1 | % Script file: lsqfit.m 2 | % 3 | % Purpose: 4 | % To perform a least-squares fit of an input data set 5 | % to a straight line, and print out the resulting slope 6 | % and intercept values. The input data for this fit 7 | % comes from a user-specified input data file. 8 | % 9 | % Record of revisions: 10 | % Date Programmer Description of change 11 | % ==== ========== ===================== 12 | % 01/30/07 S. J. Chapman Original code 13 | % 14 | % Define variables: 15 | % ii -- Loop index 16 | % n_points -- Number in input [x y] points 17 | % slope -- Slope of the line 18 | % sum_x -- Sum of all input x values 19 | % sum_x2 -- Sum of all input x values squared 20 | % sum_xy -- Sum of all input x*y yalues 21 | % sum_y -- Sum of all input y values 22 | % temp -- Variable to read user input 23 | % x -- Array of x values 24 | % x_bar -- Average x value 25 | % y -- Array of y values 26 | % y_bar -- Average y value 27 | % y_int -- y-axis intercept of the line 28 | 29 | disp('This program performs a least-squares fit of an '); 30 | disp('input data set to a straight line.'); 31 | n_points = input('Enter the number of input [x y] points: '); 32 | 33 | % Read the input data 34 | for ii = 1:n_points 35 | temp = input('Enter [x y] pair: '); 36 | x(ii) = temp(1); 37 | y(ii) = temp(2); 38 | end 39 | 40 | % Accumulate statistics 41 | sum_x = 0; 42 | sum_y = 0; 43 | sum_x2 = 0; 44 | sum_xy = 0; 45 | for ii = 1:n_points 46 | sum_x = sum_x + x(ii); 47 | sum_y = sum_y + y(ii); 48 | sum_x2 = sum_x2 + x(ii)^2; 49 | sum_xy = sum_xy + x(ii) * y(ii); 50 | end 51 | 52 | % Now calculate the slope and intercept. 53 | x_bar = sum_x / n_points; 54 | y_bar = sum_y / n_points; 55 | slope = (sum_xy - sum_x * y_bar) / ( sum_x2 - sum_x * x_bar); 56 | y_int = y_bar - slope * x_bar; 57 | 58 | % Tell user. 59 | disp('Regression coefficients for the least-squares line:'); 60 | fprintf(' Slope (m) = %8.3f\n', slope); 61 | fprintf(' Intercept (b) = %8.3f\n', y_int); 62 | fprintf(' No. of points = %8d\n', n_points); 63 | 64 | % Plot the data points as blue circles with no 65 | % connecting lines. 66 | plot(x,y,'bo'); 67 | hold on; 68 | 69 | % Create the fitted line 70 | xmin = min(x); 71 | xmax = max(x); 72 | ymin = slope * xmin + y_int; 73 | ymax = slope * xmax + y_int; 74 | 75 | % Plot a solid red line with no markers 76 | plot([xmin xmax],[ymin ymax],'r-','LineWidth',2); 77 | hold off; 78 | 79 | % Add a title and legend 80 | title ('\bfLeast-Squares Fit'); 81 | xlabel('\bf\itx'); 82 | ylabel('\bf\ity'); 83 | legend('Input data','Fitted line'); 84 | grid on 85 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap4/nested_1.m: -------------------------------------------------------------------------------- 1 | for ii = 1:3 2 | for jj = 1:3 3 | product = ii * jj; 4 | fprintf('%d * %d = %d\n',ii,jj,product); 5 | end 6 | end 7 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap4/sqr.m: -------------------------------------------------------------------------------- 1 | function x2 = sqr(x) 2 | x2 = x.*x; 3 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap4/stats_1.m: -------------------------------------------------------------------------------- 1 | % Script file: stats_1.m 2 | % 3 | % Purpose: 4 | % To calculate mean and the standard deviation of 5 | % an input data set containing an arbitrary number 6 | % of input values. 7 | % 8 | % Record of revisions: 9 | % Date Programmer Description of change 10 | % ==== ========== ===================== 11 | % 01/07/04 S. J. Chapman Original code 12 | % 13 | % Define variables: 14 | % n -- The number of input samples 15 | % std_dev -- The standard deviation of the input samples 16 | % sum_x -- The sum of the input values 17 | % sum_x2 -- The sum of the squares of the input values 18 | % x -- An input data value 19 | % xbar -- The average of the input samples 20 | 21 | % Initialize sums. 22 | n = 0; sum_x = 0; sum_x2 = 0; 23 | 24 | % Read in first value 25 | x = input('Enter first value: '); 26 | 27 | % While Loop to read input values. 28 | while x >= 0 29 | 30 | % Accumulate sums. 31 | n = n + 1; 32 | sum_x = sum_x + x; 33 | sum_x2 = sum_x2 + x^2; 34 | 35 | % Read in next value 36 | x = input('Enter next value: '); 37 | 38 | end 39 | 40 | % Calculate the mean and standard deviation 41 | x_bar = sum_x / n; 42 | std_dev = sqrt( (n * sum_x2 - sum_x^2) / (n * (n-1)) ); 43 | 44 | % Tell user. 45 | fprintf('The mean of this data set is: %f\n', x_bar); 46 | fprintf('The standard deviation is: %f\n', std_dev); 47 | fprintf('The number of data points is: %f\n', n); 48 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap4/stats_2.m: -------------------------------------------------------------------------------- 1 | % Script file: stats_2.m 2 | % 3 | % Purpose: 4 | % To calculate mean and the standard deviation of 5 | % an input data set containing an arbitrary number 6 | % of input values. 7 | % 8 | % Record of revisions: 9 | % Date Programmer Description of change 10 | % ==== ========== ===================== 11 | % 01/24/07 S. J. Chapman Original code 12 | % 1. 01/24/07 S. J. Chapman Correct divide-by-0 error if 13 | % 0 or 1 input values given. 14 | % 15 | % Define variables: 16 | % n -- The number of input samples 17 | % std_dev -- The standard deviation of the input samples 18 | % sum_x -- The sum of the input values 19 | % sum_x2 -- The sum of the squares of the input values 20 | % x -- An input data value 21 | % xbar -- The average of the input samples 22 | 23 | % Initialize sums. 24 | n = 0; sum_x = 0; sum_x2 = 0; 25 | 26 | % Read in first value 27 | x = input('Enter first value: '); 28 | 29 | % While Loop to read input values. 30 | while x >= 0 31 | 32 | % Accumulate sums. 33 | n = n + 1; 34 | sum_x = sum_x + x; 35 | sum_x2 = sum_x2 + x^2; 36 | 37 | % Read in next value 38 | x = input('Enter next value: '); 39 | 40 | end 41 | 42 | % Check to see if we have enough input data. 43 | if n < 2 % Insufficient information 44 | 45 | disp('At least 2 values must be entered!'); 46 | 47 | else % There is enough information, so 48 | % calculate the mean and standard deviation 49 | 50 | x_bar = sum_x / n; 51 | std_dev = sqrt( (n * sum_x2 - sum_x^2) / (n * (n-1)) ); 52 | 53 | % Tell user. 54 | fprintf('The mean of this data set is: %f\n', x_bar); 55 | fprintf('The standard deviation is: %f\n', std_dev); 56 | fprintf('The number of data points is: %f\n', n); 57 | 58 | end 59 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap4/stats_3.m: -------------------------------------------------------------------------------- 1 | % Script file: stats_3.m 2 | % 3 | % Purpose: 4 | % To calculate mean and the standard deviation of 5 | % an input data set, where each input value can be 6 | % positive, negative, or zero. 7 | % 8 | % Record of revisions: 9 | % Date Programmer Description of change 10 | % ==== ========== ===================== 11 | % 01/27/07 S. J. Chapman Original code 12 | % 13 | % Define variables: 14 | % ii -- Loop index 15 | % n -- The number of input samples 16 | % std_dev -- The standard deviation of the input samples 17 | % sum_x -- The sum of the input values 18 | % sum_x2 -- The sum of the squares of the input values 19 | % x -- An input data value 20 | % xbar -- The average of the input samples 21 | 22 | % Initialize sums. 23 | sum_x = 0; sum_x2 = 0; 24 | 25 | % Get the number of points to input. 26 | n = input('Enter number of points: '); 27 | 28 | % Check to see if we have enough input data. 29 | if n < 2 % Insufficient data 30 | 31 | disp ('At least 2 values must be entered.'); 32 | 33 | else % we will have enough data, so let's get it. 34 | 35 | % Loop to read input values. 36 | for ii = 1:n 37 | 38 | % Read in next value 39 | x = input('Enter value: '); 40 | 41 | % Accumulate sums. 42 | sum_x = sum_x + x; 43 | sum_x2 = sum_x2 + x^2; 44 | 45 | end 46 | 47 | % Now calculate statistics. 48 | x_bar = sum_x / n; 49 | std_dev = sqrt( (n * sum_x2 - sum_x^2) / (n * (n-1)) ); 50 | 51 | % Tell user. 52 | fprintf('The mean of this data set is: %f\n', x_bar); 53 | fprintf('The standard deviation is: %f\n', std_dev); 54 | fprintf('The number of data points is: %f\n', n); 55 | 56 | end 57 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap4/test_break.m: -------------------------------------------------------------------------------- 1 | for ii = 1:5 2 | if ii == 3 3 | break; 4 | end 5 | fprintf('ii = %d\n',ii); 6 | end 7 | disp(['End of loop!']); 8 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap4/test_continue.m: -------------------------------------------------------------------------------- 1 | for ii = 1:5 2 | if ii == 3 3 | continue; 4 | end 5 | fprintf('ii = %d\n',ii); 6 | end 7 | disp(['End of loop!']); 8 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap4/timings.m: -------------------------------------------------------------------------------- 1 | % Script file: timings.m 2 | % 3 | % Purpose: 4 | % This program calculates the time required to 5 | % calculate the squares of all integers from 1 to 6 | % 10,000 in four different ways: 7 | % 1. Using a for loop with an uninitialized output 8 | % array. 9 | % 2. Using a for loop with a pre-allocated output 10 | % array and NO JIT compiler. 11 | % 3. Using a for loop with a pre-allocated output 12 | % array and the JIT compiler. 13 | % 4. Using vectors. 14 | % 15 | % Record of revisions: 16 | % Date Programmer Description of change 17 | % ==== ========== ===================== 18 | % 01/29/07 S. J. Chapman Original code 19 | % 20 | % Define variables: 21 | % ii, jj -- Loop index 22 | % average1 -- Average time for calculation 1 23 | % average2 -- Average time for calculation 2 24 | % average3 -- Average time for calculation 3 25 | % average4 -- Average time for calculation 4 26 | % maxcount -- Number of times to loop calculation 27 | % square -- Array of squares 28 | 29 | % Perform calculation with an uninitialized array 30 | % "square". This calculation is done only once 31 | % because it is so slow. 32 | maxcount = 1; % Number of repetitions 33 | tic; % Start timer 34 | for jj = 1:maxcount 35 | clear square % Clear output array 36 | for ii = 1:10000 37 | square(ii) = ii^2; % Calculate square 38 | end 39 | end 40 | average1 = (toc)/maxcount; % Calculate average time 41 | 42 | % Perform calculation with a pre-allocated array 43 | % "square", calling an external function to square 44 | % the number. This calculation is averaged over 10 45 | % loops. 46 | maxcount = 10; % Number of repetitions 47 | tic; % Start timer 48 | for jj = 1:maxcount 49 | clear square % Clear output array 50 | square = zeros(1,10000); % Pre-initialize array 51 | for ii = 1:10000 52 | square(ii) = sqr(ii); % Calculate square 53 | end 54 | end 55 | average2 = (toc)/maxcount; % Calculate average time 56 | 57 | % Perform calculation with a pre-allocated array 58 | % "square". This calculation is averaged over 100 59 | % loops. 60 | maxcount = 100; % Number of repetitions 61 | tic; % Start timer 62 | for jj = 1:maxcount 63 | clear square % Clear output array 64 | square = zeros(1,10000); % Pre-initialize array 65 | for ii = 1:10000 66 | square(ii) = ii^2; % Calculate square 67 | end 68 | end 69 | average3 = (toc)/maxcount; % Calculate average time 70 | 71 | % Perform calculation with vectors. This calculation 72 | % averaged over 1000 executions. 73 | maxcount = 1000; % Number of repetitions 74 | tic; % Start timer 75 | for jj = 1:maxcount 76 | clear square % Clear output array 77 | ii = 1:10000; % Set up vector 78 | square = ii.^2; % Calculate square 79 | end 80 | average4 = (toc)/maxcount; % Calculate average time 81 | 82 | % Display results 83 | fprintf('Loop / uninitialized array = %8.4f\n', average1); 84 | fprintf('Loop / initialized array / no JIT = %8.4f\n', average2); 85 | fprintf('Loop / initialized array / JIT = %8.4f\n', average3); 86 | fprintf('Vectorized = %8.4f\n', average4); 87 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap5/dist2.m: -------------------------------------------------------------------------------- 1 | function distance = dist2(x1, y1, x2, y2) 2 | %DIST2 Calculate the distance between two points 3 | % Function DIST2 calculates the distance between 4 | % two points (x1,y1) and (x2,y2) in a Cartesian 5 | % coordinate system. 6 | % 7 | % Calling sequence: 8 | % distance = dist2(x1, y1, x2, y2) 9 | 10 | % Define variables: 11 | % x1 -- x-position of point 1 12 | % y1 -- y-position of point 1 13 | % x2 -- x-position of point 2 14 | % y2 -- y-position of point 2 15 | % distance -- Distance between points 16 | 17 | % Record of revisions: 18 | % Date Programmer Description of change 19 | % ==== ========== ===================== 20 | % 02/01/07 S. J. Chapman Original code 21 | 22 | % Calculate distance. 23 | distance = sqrt((x2-x1).^2 + (y2-y1).^2); 24 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap5/fact.m: -------------------------------------------------------------------------------- 1 | function res = fact(n) 2 | 3 | res = 1; 4 | for ii = 2:n 5 | res = res * ii; 6 | end 7 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap5/fig5_12c.m: -------------------------------------------------------------------------------- 1 | ray = sqrt(randn(1,10000000).^2 + randn(1,10000000).^2); 2 | ray = ray / mean(ray) * 10; 3 | [n,x]=hist(ray,0:1:34); 4 | figure(3); 5 | 6 | n = n/size(ray); 7 | plot(x,n,'LineWidth',2); 8 | hold on; 9 | plot([10 10],get(gca,'Ylim'),'k-.'); 10 | plot([26 26],get(gca,'Ylim'),'r:'); 11 | legend('Noise probability distribution','Mean Noise','Detection Threshold'); 12 | title('\bfNoise distribution after detection'); 13 | xlabel('\bfAmplitude (volts)'); 14 | ylabel('\bfProbability'); 15 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap5/mystats.m: -------------------------------------------------------------------------------- 1 | function [avg, med] = mystats(u) 2 | %MYSTATS Find mean and median with internal functions. 3 | % Function MYSTATS calculates the average and median 4 | % of a data set using subfunctions. 5 | 6 | n = length(u); 7 | avg = mean(u,n); 8 | med = median(u,n); 9 | 10 | 11 | function a = mean(v,n) 12 | % Subfunction to calculate average. 13 | a = sum(v)/n; 14 | 15 | 16 | function m = median(v,n) 17 | % Subfunction to calculate median. 18 | w = sort(v); 19 | if rem(n,2) == 1 20 | m = w((n+1)/2); 21 | else 22 | m = (w(n/2)+w(n/2+1))/2; 23 | end 24 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap5/polar2rect.m: -------------------------------------------------------------------------------- 1 | function [x, y] = polar2rect(r,theta) 2 | %POLAR2RECT Convert rectangular to polar coordinates 3 | % Function POLAR2RECT accepts the polar coordinates 4 | % (r,theta), where theta is expressed in degrees, 5 | % and converts them into the rectangular coordinates 6 | % (x,y). 7 | % 8 | % Calling sequence: 9 | % [x, y] = polar2rect(r,theta) 10 | 11 | % Define variables: 12 | % r -- Length of polar vector 13 | % theta -- Angle of vector in degrees 14 | % x -- x-position of point 15 | % y -- y-position of point 16 | 17 | % Record of revisions: 18 | % Date Programmer Description of change 19 | % ==== ========== ===================== 20 | % 02/01/07 S. J. Chapman Original code 21 | 22 | x = r * cos(theta * pi/180); 23 | y = r * sin(theta * pi/180); 24 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap5/polar_value.m: -------------------------------------------------------------------------------- 1 | function [mag, angle] = polar_value(x,y) 2 | %POLAR_VALUE Converts (x,y) to (r,theta) 3 | % Function POLAR_VALUE converts an input (x,y) 4 | % value into (r,theta), with theta in degrees. 5 | % It illustrates the use of optional arguments. 6 | 7 | % Define variables: 8 | % angle -- Angle in degrees 9 | % msg -- Error message 10 | % mag -- Magnitude 11 | % x -- Input x value 12 | % y -- Input y value (optional) 13 | % 14 | % Record of revisions: 15 | % Date Programmer Description of change 16 | % ==== ========== ===================== 17 | % 02/03/07 S. J. Chapman Original code 18 | 19 | % Check for a legal number of input arguments. 20 | msg = nargchk(1,2,nargin); 21 | error(msg); 22 | 23 | % If the y argument is missing, set it to 0. 24 | if nargin < 2 25 | y = 0; 26 | end 27 | 28 | % Check for (0,0) input arguments, and print out 29 | % a warning message. 30 | if x == 0 & y == 0 31 | msg = 'Both x and y are zero: angle is meaningless!'; 32 | warning(msg); 33 | end 34 | 35 | % Now calculate the magnitude. 36 | mag = sqrt(x.^2 + y.^2); 37 | 38 | % If the second output argument is present, calculate 39 | % angle in degrees. 40 | if nargout == 2 41 | angle = atan2(y,x) * 180/pi; 42 | end 43 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap5/private/sin.m: -------------------------------------------------------------------------------- 1 | function res = sin(x) 2 | res = 9; 3 | end % fucntion sin -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap5/private/sin1.m: -------------------------------------------------------------------------------- 1 | function res = sin1(x) 2 | res = 9; 3 | end % function sin1 -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap5/quickplot.m: -------------------------------------------------------------------------------- 1 | function quickplot(fun,xlim) 2 | %QUICKPLOT Generate quick plot of a function 3 | % Function QUICKPLOT generates a quick plot 4 | % of a function contained in a external m-file, 5 | % between user-specified x limits. 6 | 7 | % Define variables: 8 | % fun -- Name of function to plot in a char string 9 | % msg -- Error message 10 | % n_steps -- Number of steps to plot 11 | % step_size -- Step size 12 | % x -- X-values to plot 13 | % y -- Y-values to plot 14 | % xlim -- Plot x limits 15 | % 16 | % Record of revisions: 17 | % Date Programmer Description of change 18 | % ==== ========== ===================== 19 | % 02/07/07 S. J. Chapman Original code 20 | 21 | % Check for a legal number of input arguments. 22 | msg = nargchk(2,2,nargin); 23 | error(msg); 24 | 25 | % Check the second argument to see if it has two 26 | % elements. Note that this double test allows the 27 | % argument to be either a row or a column vector. 28 | if ( size(xlim,1) == 1 & size(xlim,2) == 2 ) | ... 29 | ( size(xlim,1) == 2 & size(xlim,2) == 1 ) 30 | 31 | % Ok--continue processing. 32 | n_steps = 100; 33 | step_size = (xlim(2) - xlim(1)) / n_steps; 34 | x = xlim(1):step_size:xlim(2); 35 | y = feval(fun,x); 36 | plot(x,y); 37 | title(['\bfPlot of function ' fun '(x)']); 38 | xlabel('\bfx'); 39 | ylabel(['\bf' fun '(x)']); 40 | 41 | else 42 | % Else wrong number of elements in xlim. 43 | error('Incorrect number of elements in xlim.'); 44 | end 45 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap5/random0.m: -------------------------------------------------------------------------------- 1 | function ran = random0(m,n) 2 | %RANDOM0 Generate uniform random numbers in [0,1) 3 | % Function RANDOM0 generates an array of uniform 4 | % random numbers in the range [0,1). The usage 5 | % is: 6 | % 7 | % random0(m) -- Generate an m x m array 8 | % random0(m,n) -- Generate an m x n array 9 | 10 | % Define variables: 11 | % ii -- Index variable 12 | % iseed -- Random number seed (global) 13 | % jj -- Index variable 14 | % m -- Number of columns 15 | % msg -- Error message 16 | % n -- Number of rows 17 | % ran -- Output array 18 | % 19 | % Record of revisions: 20 | % Date Programmer Description of change 21 | % ==== ========== ===================== 22 | % 02/04/07 S. J. Chapman Original code 23 | 24 | % Declare global values 25 | global iseed % Seed for random number generator 26 | 27 | % Check for a legal number of input arguments. 28 | msg = nargchk(1,2,nargin); 29 | error(msg); 30 | 31 | % If the n argument is missing, set it to m. 32 | if nargin < 2 33 | n = m; 34 | end 35 | 36 | % Initialize the output array 37 | ran = zeros(m,n); 38 | 39 | % Now calculate random values 40 | for ii = 1:m 41 | for jj = 1:n 42 | iseed = mod(8121*iseed + 28411, 134456 ); 43 | ran(ii,jj) = iseed / 134456; 44 | end 45 | end 46 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap5/rect2polar.m: -------------------------------------------------------------------------------- 1 | function [r, theta] = rect2polar(x,y) 2 | %RECT2POLAR Convert rectangular to polar coordinates 3 | % Function RECT2POLAR accepts the rectangular coordinates 4 | % (x,y) and converts them into the polar coordinates 5 | % (r,theta), where theta is expressed in degrees. 6 | % 7 | % Calling sequence: 8 | % [r, theta] = rect2polar(x,y) 9 | 10 | % Define variables: 11 | % r -- Length of polar vector 12 | % theta -- Angle of vector in degrees 13 | % x -- x-position of point 14 | % y -- y-position of point 15 | 16 | % Record of revisions: 17 | % Date Programmer Description of change 18 | % ==== ========== ===================== 19 | % 02/01/07 S. J. Chapman Original code 20 | 21 | r = sqrt( x.^2 + y .^2 ); 22 | theta = 180/pi * atan2(y,x); 23 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap5/runstats.m: -------------------------------------------------------------------------------- 1 | function [ave, std] = runstats(x) 2 | %RUNSTATS Generate running ave / std deviation 3 | % Function RUNSTATS generates a running average 4 | % and standard deviation of a data set. The 5 | % values x must be passed to this function one 6 | % at a time. A call to RUNSTATS with the argument 7 | % 'reset' will reset tue running sums. 8 | 9 | % Define variables: 10 | % ave -- Running average 11 | % msg -- Error message 12 | % n -- Number of data values 13 | % std -- Running standard deviation 14 | % sum_x -- Running sum of data values 15 | % sum_x2 -- Running sum of data values squared 16 | % x -- Input value 17 | % 18 | % Record of revisions: 19 | % Date Programmer Description of change 20 | % ==== ========== ===================== 21 | % 02/05/07 S. J. Chapman Original code 22 | 23 | % Declare persistent values 24 | persistent n % Number of input values 25 | persistent sum_x % Running sum of values 26 | persistent sum_x2 % Running sum of values squared 27 | 28 | % Check for a legal number of input arguments. 29 | msg = nargchk(1,1,nargin); 30 | error(msg); 31 | 32 | % If the argument is 'reset', reset the running sums. 33 | if x == 'reset' 34 | n = 0; 35 | sum_x = 0; 36 | sum_x2 = 0; 37 | else 38 | n = n + 1; 39 | sum_x = sum_x + x; 40 | sum_x2 = sum_x2 + x^2; 41 | end 42 | 43 | % Calculate ave and sd 44 | if n == 0 45 | ave = 0; 46 | std = 0; 47 | elseif n == 1 48 | ave = sum_x; 49 | std = 0; 50 | else 51 | ave = sum_x / n; 52 | std = sqrt((n*sum_x2 - sum_x^2) / (n*(n-1))); 53 | end 54 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap5/sample.m: -------------------------------------------------------------------------------- 1 | function out = sample(a, b, c) 2 | fprintf('In sample: a = %f, b = %f %f\n',a,b); 3 | a = b(1) + 2*a; 4 | b = a .* b; 5 | out = a + b(1); 6 | fprintf('In sample: a = %f, b = %f %f\n',a,b); 7 | 8 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap5/seed.m: -------------------------------------------------------------------------------- 1 | function seed(new_seed) 2 | %SEED Set new seed for function RANDOM0 3 | % Function SEED sets a new seed for function 4 | % RANDOM0. The new seed should be a positive 5 | % integer. 6 | 7 | % Define variables: 8 | % iseed -- Random number seed (global) 9 | % new_seed -- New seed 10 | % 11 | % Record of revisions: 12 | % Date Programmer Description of change 13 | % ==== ========== ===================== 14 | % 02/04/07 S. J. Chapman Original code 15 | 16 | % Declare global values 17 | global iseed % Seed for random number generator 18 | 19 | % Check for a legal number of input arguments. 20 | msg = nargchk(1,1,nargin); 21 | error(msg); 22 | 23 | % Save seed 24 | new_seed = round(new_seed); 25 | iseed = abs(new_seed); 26 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap5/ssort.m: -------------------------------------------------------------------------------- 1 | function out = ssort(a) 2 | %SSORT Selection sort data in ascending order 3 | % Function SSORT sorts a numeric data set into 4 | % ascending order. Note that the selection sort 5 | % is relatively inefficient. DO NOT USE THIS 6 | % FUNCTION FOR LARGE DATA SETS. Use MATLAB's 7 | % "sort" function instead. 8 | 9 | % Define variables: 10 | % a -- Input array to sort 11 | % ii -- Index variable 12 | % iptr -- Pointer to min value 13 | % jj -- Index variable 14 | % nvals -- Number of values in "a" 15 | % out -- Sorted output array 16 | % temp -- Temp variable for swapping 17 | 18 | % Record of revisions: 19 | % Date Programmer Description of change 20 | % ==== ========== ===================== 21 | % 02/02/07 S. J. Chapman Original code 22 | 23 | % Get the length of the array to sort 24 | nvals = size(a,2); 25 | 26 | % Sort the input array 27 | for ii = 1:nvals-1 28 | 29 | % Find the minimum value in a(ii) through a(n) 30 | iptr = ii; 31 | for jj = ii+1:nvals 32 | if a(jj) < a(iptr) 33 | iptr = jj; 34 | end 35 | end 36 | 37 | % iptr now points to the minimum value, so swap a(iptr) 38 | % with a(ii) if ii ~= iptr. 39 | if ii ~= iptr 40 | temp = a(ii); 41 | a(ii) = a(iptr); 42 | a(iptr) = temp; 43 | end 44 | 45 | end 46 | 47 | % Pass data back to caller 48 | out = a; 49 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap5/test_dist2.m: -------------------------------------------------------------------------------- 1 | % Script file: test_dist2.m 2 | % 3 | % Purpose: 4 | % This program tests function dist2. 5 | % 6 | % Record of revisions: 7 | % Date Programmer Description of change 8 | % ==== ========== ===================== 9 | % 02/01/07 S. J. Chapman Original code 10 | % 11 | % Define variables: 12 | % ax -- x-position of point a 13 | % ay -- y-position of point a 14 | % bx -- x-position of point b 15 | % by -- y-position of point b 16 | % result -- Distance between the points 17 | 18 | % Get input data. 19 | disp('Calculate the distance between two points:'); 20 | ax = input('Enter x value of point a: '); 21 | ay = input('Enter y value of point a: '); 22 | bx = input('Enter x value of point b: '); 23 | by = input('Enter y value of point b: '); 24 | 25 | % Evaluate function 26 | result = dist2 (ax, ay, bx, by); 27 | 28 | % Write out result. 29 | fprintf('The distance between points a and b is %f\n',result); 30 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap5/test_nested_1.m: -------------------------------------------------------------------------------- 1 | function res = test_nested_1 2 | 3 | % This is the top level function. 4 | % Define some variables. 5 | a = 1; b = 2; x = 0; y = 9; 6 | 7 | % Display variables before call to fun1 8 | fprintf('Before call to fun1:\n'); 9 | fprintf('a, b, x, y = %2d %2d %2d %2d\n', a, b, x, y); 10 | 11 | % Call nested function fun1 12 | x = fun1(x); 13 | 14 | % Display variables after call to fun1 15 | fprintf('\nAfter call to fun1:\n'); 16 | fprintf('a, b, x, y = %2d %2d %2d %2d\n', a, b, x, y); 17 | 18 | % Declare a nested function 19 | function res = fun1(y) 20 | 21 | % Display variables at start of call to fun1 22 | fprintf('\nAt start of call to fun1:\n'); 23 | fprintf('a, b, x, y = %2d %2d %2d %2d\n', a, b, x, y); 24 | 25 | y = y + 5; 26 | a = a + 1; 27 | res = y; 28 | 29 | % Display variables at end of call to fun1 30 | fprintf('\nAt end of call to fun1:\n'); 31 | fprintf('a, b, x, y = %2d %2d %2d %2d\n', a, b, x, y); 32 | 33 | end % function fun1 34 | 35 | end % function test_nested_1 36 | 37 | 38 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap5/test_nested_2.m: -------------------------------------------------------------------------------- 1 | function res = test_nested_1 2 | 3 | % This is the top level function. 4 | % Define some variables. 5 | a = 1; 6 | b = 2; 7 | x = 0; 8 | y = 9; 9 | 10 | % Display variables before call to fun1 11 | disp('Before call to fun1:') 12 | disp(['a = ' num2str(a)]); 13 | disp(['b = ' num2str(b)]); 14 | disp(['x = ' num2str(x)]); 15 | disp(['y = ' num2str(y)]); 16 | 17 | % Call fun1 18 | x = fun1(x); 19 | 20 | % Display variables after call to fun1 21 | disp(' '); 22 | disp('After call to fun1:') 23 | disp(['a = ' num2str(a)]); 24 | disp(['b = ' num2str(b)]); 25 | disp(['x = ' num2str(x)]); 26 | disp(['y = ' num2str(y)]); 27 | 28 | % Declare a nested function 29 | function res = fun1(y) 30 | res = y + a; 31 | a = a + 1; 32 | 33 | % Display variables inside call to fun1 34 | disp(' '); 35 | disp('Inside call to fun1:') 36 | disp(['a = ' num2str(a)]); 37 | disp(['b = ' num2str(b)]); 38 | disp(['x = ' num2str(x)]); 39 | disp(['y = ' num2str(y)]); 40 | 41 | end % function fun1 42 | 43 | end % function test_nested_1 44 | 45 | 46 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap5/test_runstats.m: -------------------------------------------------------------------------------- 1 | % Script file: test_runstats.m 2 | % 3 | % Purpose: 4 | % To read in an input data set andn calculate the 5 | % running statistics on the data set as the values 6 | % are read in. The running stats will be written 7 | % to the Command window. 8 | % 9 | % Record of revisions: 10 | % Date Programmer Description of change 11 | % ==== ========== ===================== 12 | % 02/05/07 S. J. Chapman Original code 13 | % 14 | % Define variables: 15 | % array -- Input data array 16 | % ave -- Running average 17 | % std -- Running standard deviation 18 | % ii -- Index variable 19 | % nvals -- Number of input values 20 | % std -- Running standard deviation 21 | 22 | % First reset running sums 23 | [ave std] = runstats('reset'); 24 | 25 | % Prompt for the number of values in the data set 26 | nvals = input('Enter number of values in data set: '); 27 | 28 | % Get input values 29 | for ii = 1:nvals 30 | 31 | % Prompt for next value 32 | string = ['Enter value ' int2str(ii) ': ']; 33 | x = input(string); 34 | 35 | % Get running statistics 36 | [ave std] = runstats(x); 37 | 38 | % Display running statistics 39 | fprintf('Average = %8.4f; Std dev = %8.4f\n',ave, std); 40 | 41 | end 42 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap5/test_sample.m: -------------------------------------------------------------------------------- 1 | a = 2; b = [6 4]; 2 | fprintf('Before sample: a = %f, b = %f %f\n',a,b); 3 | out = sample(a,b); 4 | fprintf('After sample: a = %f, b = %f %f\n',a,b); 5 | fprintf('After sample: out = %f\n',out); 6 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap5/test_ssort.m: -------------------------------------------------------------------------------- 1 | % Script file: test_ssort.m 2 | % 3 | % Purpose: 4 | % To read in an input data set, sort it into ascending 5 | % order using the selection sort algorithm, and to 6 | % write the sorted data to the Command window. This 7 | % program calls function "ssort" to do the actual 8 | % sorting. 9 | % 10 | % Record of revisions: 11 | % Date Programmer Description of change 12 | % ==== ========== ===================== 13 | % 02/02/07 S. J. Chapman Original code 14 | % 15 | % Define variables: 16 | % array -- Input data array 17 | % ii -- Index variable 18 | % nvals -- Number of input values 19 | % sorted -- Sorted data array 20 | 21 | % Prompt for the number of values in the data set 22 | nvals = input('Enter number of values to sort: '); 23 | 24 | % Preallocate array 25 | array = zeros(1,nvals); 26 | 27 | % Get input values 28 | for ii = 1:nvals 29 | 30 | % Prompt for next value 31 | string = ['Enter value ' int2str(ii) ': ']; 32 | array(ii) = input(string); 33 | 34 | end 35 | 36 | % Now sort the data 37 | sorted = ssort(array); 38 | 39 | % Display the sorted result. 40 | fprintf('\nSorted data:\n'); 41 | for ii = 1:nvals 42 | fprintf(' %8.4f\n',sorted(ii)); 43 | end 44 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap6/bar_plot.m: -------------------------------------------------------------------------------- 1 | x = [ 1 2 3 4 5 6]; 2 | y = [ 2 6 8 7 8 5]; 3 | bar(x,y); 4 | title('\bfExample of a Bar Plot'); 5 | xlabel('\bf\itx'); 6 | ylabel('\bf\ity'); 7 | axis([0 7 0 10]); 8 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap6/barh_plot.m: -------------------------------------------------------------------------------- 1 | x = [ 1 2 3 4 5 6]; 2 | y = [ 2 6 8 7 8 5]; 3 | barh(x,y); 4 | title('\bfExample of a Horizontal Bar Plot'); 5 | xlabel('\bf\ity'); 6 | ylabel('\bf\itx'); 7 | axis([0 10 0 7]); 8 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap6/c_strcmp.m: -------------------------------------------------------------------------------- 1 | function result = c_strcmp(str1,str2) 2 | %C_STRCMP Compare strings like C function "strcmp" 3 | % Function C_STRCMP compares two strings, and returns 4 | % a -1 if str1 < str2, a 0 if str1 == str2, and a 5 | % +1 if str1 > str2. 6 | 7 | % Define variables: 8 | % diff -- Logical array of string differences 9 | % msg -- Error message 10 | % result -- Result of function 11 | % str1 -- First string to compare 12 | % str2 -- Second string to compare 13 | % strings -- Padded array of strings 14 | % 15 | % Record of revisions: 16 | % Date Programmer Description of change 17 | % ==== ========== ===================== 18 | % 02/25/07 S. J. Chapman Original code 19 | 20 | % Check for a legal number of input arguments. 21 | msg = nargchk(2,2,nargin); 22 | error(msg); 23 | 24 | % Check to see if the arguments are strings 25 | if ~(isstr(str1) & isstr(str2)) 26 | error('Both str1 and str2 must both be strings!') 27 | else 28 | 29 | % Pad strings 30 | strings = strvcat(str1,str2); 31 | 32 | % Compare strings 33 | diff = strings(1,:) ~= strings(2,:); 34 | if sum(diff) == 0 35 | 36 | % Strings match, so return a zero! 37 | result = 0; 38 | else 39 | % Find first difference between strings 40 | ival = find(diff); 41 | if strings(1,ival(1)) > strings(2,ival(1)) 42 | result = 1; 43 | else 44 | result = -1; 45 | end 46 | end 47 | end 48 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap6/calc_roots2.m: -------------------------------------------------------------------------------- 1 | % Script file: calc_roots2.m 2 | % 3 | % Purpose: 4 | % This program solves for the roots of a quadratic equation 5 | % of the form a*x**2 + b*x + c = 0. It calculates the answers 6 | % regardless of the type of roots that the equation possesses. 7 | % 8 | % Record of revisions: 9 | % Date Programmer Description of change 10 | % ==== ========== ===================== 11 | % 02/24/07 S. J. Chapman Original code 12 | % 13 | % Define variables: 14 | % a -- Coefficient of x^2 term of equation 15 | % b -- Coefficient of x term of equation 16 | % c -- Constant term of equation 17 | % discriminant -- Discriminant of the equation 18 | % x1 -- First solution of equation 19 | % x2 -- Second solution of equation 20 | 21 | % Prompt the user for the coefficients of the equation 22 | disp ('This program solves for the roots of a quadratic '); 23 | disp ('equation of the form A*X^2 + B*X + C = 0. '); 24 | a = input ('Enter the coefficient A: '); 25 | b = input ('Enter the coefficient B: '); 26 | c = input ('Enter the coefficient C: '); 27 | 28 | % Calculate discriminant 29 | discriminant = b^2 - 4 * a * c; 30 | 31 | % Solve for the roots 32 | x1 = ( -b + sqrt(discriminant) ) / ( 2 * a ); 33 | x2 = ( -b - sqrt(discriminant) ) / ( 2 * a ); 34 | 35 | % Display results 36 | disp ('The roots of this equation are:'); 37 | fprintf ('x1 = (%f) +i (%f)\n', real(x1), imag(x1)); 38 | fprintf ('x2 = (%f) +i (%f)\n', real(x2), imag(x2)); 39 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap6/compass_plot.m: -------------------------------------------------------------------------------- 1 | x = [ 2 -2 1 3 -1]; 2 | y = [ 2 2 -2 1 -3]; 3 | compass(x,y); 4 | title('\bfExample of a Compass Plot'); 5 | grid on 6 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap6/fig6_12a_test_mesh.m: -------------------------------------------------------------------------------- 1 | [x,y] = meshgrid(-4:0.2:4); 2 | z = exp(-0.5*(x.^2+0.5*(x-y).^2)); 3 | mesh(x,y,z); 4 | title('\bfMesh Plot'); 5 | xlabel('\bfx'); 6 | ylabel('\bfy'); 7 | 8 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap6/fig6_12b_test_surf.m: -------------------------------------------------------------------------------- 1 | [x,y] = meshgrid(-4:0.2:4); 2 | z = exp(-0.5*(x.^2+0.5*(x-y).^2)); 3 | surf(x,y,z); 4 | title('\bfSurface Plot'); 5 | xlabel('\bfx'); 6 | ylabel('\bfy'); 7 | zlabel('\bfz'); 8 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap6/fig6_12c_test_contour.m: -------------------------------------------------------------------------------- 1 | [x,y] = meshgrid(-4:0.2:4); 2 | z = exp(-0.5*(x.^2+0.5*(x-y).^2)); 3 | contour(x,y,z); 4 | title('\bfContour Plot'); 5 | xlabel('\bfx'); 6 | ylabel('\bfy'); 7 | zlabel('\bfz'); 8 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap6/pie_plot.m: -------------------------------------------------------------------------------- 1 | data = [ 10 37 5 6 6]; 2 | explode = [ 0 1 0 0 0]; 3 | pie(data,explode); 4 | title('\bfExample of a Pie Plot'); 5 | legend('One','Two','Three','Four','Five'); 6 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap6/stair_plot.m: -------------------------------------------------------------------------------- 1 | x = [ 1 2 3 4 5 6]; 2 | y = [ 2 6 8 7 8 5]; 3 | stairs(x,y); 4 | title('\bfExample of a Stair Plot'); 5 | xlabel('\bf\itx'); 6 | ylabel('\bf\ity'); 7 | axis([0 7 0 10]); 8 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap6/stem_plot.m: -------------------------------------------------------------------------------- 1 | x = [ 1 2 3 4 5 6]; 2 | y = [ 2 6 8 7 8 5]; 3 | stem(x,y); 4 | title('\bfExample of a Stem Plot'); 5 | xlabel('\bf\itx'); 6 | ylabel('\bf\ity'); 7 | axis([0 7 0 10]); 8 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap6/test_contour2.m: -------------------------------------------------------------------------------- 1 | [x,y] = meshgrid(-4:0.2:4); 2 | z = exp(-0.5*(x.^2+0.5*(x-y).^2)); 3 | [c,h]=contour(x,y,z); 4 | clabel(c,h); 5 | title('\bfContour Plot'); 6 | xlabel('\bfx'); 7 | ylabel('\bfy'); 8 | zlabel('\bfz'); 9 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap6/test_ezplot.m: -------------------------------------------------------------------------------- 1 | ezplot('sin(x)/x',[-4*pi 4*pi]); 2 | title('Plot of sin x / x'); 3 | grid on; 4 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap6/test_fplot.m: -------------------------------------------------------------------------------- 1 | fplot('sin(x)/x',[-4*pi 4*pi]); 2 | title('\bfPlot of sin \itx \rm\bf / \itx'); 3 | grid on; 4 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap6/test_hist.m: -------------------------------------------------------------------------------- 1 | y = randn(10000,1); 2 | hist(y,15); 3 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap6/test_plot.m: -------------------------------------------------------------------------------- 1 | t = 0:0.1:10; 2 | x = exp(-0.2*t) .* cos(2*t); 3 | y = exp(-0.2*t) .* sin(2*t); 4 | plot(x,y,'LineWidth',2); 5 | title('\bfTwo-Dimensional Line Plot'); 6 | xlabel('\bfx'); 7 | ylabel('\bfy'); 8 | axis square; 9 | grid on; 10 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap6/test_plot3.m: -------------------------------------------------------------------------------- 1 | t = 0:0.1:10; 2 | x = exp(-0.2*t) .* cos(2*t); 3 | y = exp(-0.2*t) .* sin(2*t); 4 | plot3(x,y,t,'LineWidth',2); 5 | title('\bfThree-Dimensional Line Plot'); 6 | xlabel('\bfx'); 7 | ylabel('\bfy'); 8 | zlabel('\bftime'); 9 | grid on; 10 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap7/calc_average.m: -------------------------------------------------------------------------------- 1 | function [ave, nvals] = calc_average(structure,field) 2 | %CALC_AVERAGE Calculate the average of values in a field. 3 | % Function CALC_AVERAGE calculates the average value 4 | % of the elements in a particular field of a structure 5 | % array. It returns the average value and (optionally) 6 | % the number of items averaged. 7 | 8 | % Define variables: 9 | % arr -- Array of values to average 10 | % ave -- Average of arr 11 | % ii -- Index variable 12 | % 13 | % Record of revisions: 14 | % Date Programmer Description of change 15 | % ==== ========== ===================== 16 | % 03/04/07 S. J. Chapman Original code 17 | % 18 | % Check for a legal number of input arguments. 19 | msg = nargchk(2,2,nargin); 20 | error(msg); 21 | 22 | % Create an array of values from the field 23 | arr = []; 24 | for ii = 1:length(structure) 25 | arr = [arr structure(ii).(field)]; 26 | end 27 | 28 | % Calculate average 29 | ave = mean(arr); 30 | 31 | % Return number of values averaged 32 | if nargout == 2 33 | nvals = length(arr); 34 | end 35 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap7/calc_decay.m: -------------------------------------------------------------------------------- 1 | % Script file: calc_decay.m 2 | % 3 | % Purpose: 4 | % This program calculates the amount of Thorium 227 and 5 | % Radium 223 left as a function of time, given an inital 6 | % concentration of 1 gram of Thorium 227 and no grams of 7 | % Radium 223. 8 | % 9 | % Record of revisions: 10 | % Date Programmer Description of change 11 | % ==== ========== ===================== 12 | % 03/15/07 S. J. Chapman Original code 13 | % 14 | % Define variables: 15 | % odefun_handle -- Handle to function that defines the derivative 16 | % tspan -- Duration to solve equation for 17 | % yo -- Initial condition for equation 18 | % t -- Array of solution times 19 | % y -- Array of solution values 20 | 21 | % Get a handle to the function that defines the derivative. 22 | odefun_handle = @decay1; 23 | 24 | % Solve the equation over the period 0 to 100 days 25 | tspan = [0 100]; 26 | 27 | % Set the initial conditions 28 | y0(1) = 1000000; % Atoms of Thorium 227 29 | y0(2) = 0; % Atoms of Radium 223 30 | 31 | % Call the differential equation solver. 32 | [t,y] = ode45(odefun_handle,tspan,y0); 33 | 34 | % Plot the result 35 | figure(1); 36 | plot(t,y(:,1),'b-','LineWidth',2); 37 | hold on; 38 | plot(t,y(:,2),'k--','LineWidth',2); 39 | title('\bfAmount of Thorium 227 and Radium 223 vs Time'); 40 | xlabel('\bfTime (days)'); 41 | ylabel('\bfNumber of Atoms'); 42 | legend('Thorium 227','Radium 223'); 43 | grid on; 44 | hold off; 45 | 46 | 47 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap7/count_calls.m: -------------------------------------------------------------------------------- 1 | function fhandle = count_calls(initial_value) 2 | 3 | % Save initial value in a local variable 4 | % in the host function. 5 | current_count = initial_value; 6 | 7 | % Create and return a function handle to the 8 | % nested function below. 9 | fhandle = @increment_count; 10 | 11 | % Define a nested function to increment counter 12 | function count = increment_count 13 | current_count = current_count + 1; 14 | count = current_count; 15 | end % function increment_count 16 | 17 | end % function count_calls 18 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap7/decay1.m: -------------------------------------------------------------------------------- 1 | function yprime = decay1(t,y) 2 | %DECAY1 Calculates the decay rates of Thorium 227 and Radium 223. 3 | % Function DECAY1 Calculates the rates of change of Thorium 227 4 | % and Radium 223 (yprime) for a given current concentration y. 5 | 6 | % Define variables: 7 | % t -- Time (in days) 8 | % y -- Vector of current concentrations 9 | % 10 | % Record of revisions: 11 | % Date Programmer Description of change 12 | % ==== ========== ===================== 13 | % 03/15/07 S. J. Chapman Original code 14 | 15 | % Set decay constants. 16 | lambda_th = 0.03710636; 17 | lambda_ra = 0.0606428; 18 | 19 | % Calculate rates of decay 20 | yprime = zeros(2,1); 21 | yprime(1) = -lambda_th * y(1); 22 | yprime(2) = -lambda_ra * y(2) + lambda_th * y(1); 23 | 24 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap7/fact1.m: -------------------------------------------------------------------------------- 1 | function res = fact1(x) 2 | if x > 0 3 | res = x * fact1(x-1); 4 | else 5 | res = 1; 6 | end -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap7/fun1.m: -------------------------------------------------------------------------------- 1 | function yprime = fun1(t,y) 2 | yprime = -2 * y; 3 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap7/gensparse.m: -------------------------------------------------------------------------------- 1 | z = eye(1000); 2 | z1 = rand(1000); 3 | for ii = 1:999 4 | for jj = ii+1:1000 5 | if z1(ii,jj) <= 0.003 6 | z(ii,jj) = -300 * z1(ii,jj); 7 | z(jj,ii) = z(ii,jj); 8 | end 9 | end 10 | end -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap7/my_func.m: -------------------------------------------------------------------------------- 1 | function res = my_func(x) 2 | res = x.^2 - 2*x + 1; 3 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap7/ode45_test1.m: -------------------------------------------------------------------------------- 1 | % Script file: ode45_test1.m 2 | % 3 | % Purpose: 4 | % This program solves a differential equation of the 5 | % form dy/dt + 2 * y = 0, with the initial condition 6 | % y(0) = 1. 7 | % 8 | % Record of revisions: 9 | % Date Programmer Description of change 10 | % ==== ========== ===================== 11 | % 03/15/07 S. J. Chapman Original code 12 | % 13 | % Define variables: 14 | % odefun_handle -- Handle to function that defines the derivative 15 | % tspan -- Duration to solve equation for 16 | % yo -- Initial condition for equation 17 | % t -- Array of solution times 18 | % y -- Array of solution values 19 | 20 | % Get a handle to the function that defines the 21 | % derivative. 22 | odefun_handle = @fun1; 23 | 24 | % Solve the equation over the period 0 to 5 seconds 25 | tspan = [0 5]; 26 | 27 | % Set the initial conditions 28 | y0 = 1; 29 | 30 | % Call the differential equation solver. 31 | [t,y] = ode45(odefun_handle,tspan,y0); 32 | 33 | % Plot the result 34 | figure(1); 35 | plot(t,y,'b-','LineWidth',2); 36 | grid on; 37 | title('\bfSolution of Differential Equation'); 38 | xlabel('\bfTime (s)'); 39 | ylabel('\bf\ity'''); 40 | 41 | 42 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap7/plotfunc.m: -------------------------------------------------------------------------------- 1 | function plotfunc(fun,points) 2 | %PLOTFUNC Plots a function between the specified points. 3 | % Function PLOTFUNC accepts a function handle, and 4 | % plots the function at the points specified. 5 | 6 | % Define variables: 7 | % fun -- Function handle 8 | % msg -- Error message 9 | % 10 | % Record of revisions: 11 | % Date Programmer Description of change 12 | % ==== ========== ===================== 13 | % 03/05/07 S. J. Chapman Original code 14 | 15 | % Check for a legal number of input arguments. 16 | msg = nargchk(2,2,nargin); 17 | error(msg); 18 | 19 | % Get function name 20 | fname = func2str(fun); 21 | 22 | % Plot the data and label the plot 23 | plot(points,fun(points)); 24 | title(['\bfPlot of ' fname '(x) vs x']); 25 | xlabel('\bfx'); 26 | ylabel(['\bf' fname '(x)']); 27 | grid on; 28 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap7/plotline.m: -------------------------------------------------------------------------------- 1 | function plotline(varargin) 2 | %PLOTLINE Plot points specified by [x,y] pairs. 3 | % Function PLOTLINE accepts an arbitrary number of 4 | % [x,y] points and plots a line connecting them. 5 | % In addition, it can accept a line specification 6 | % string, and pass that string on to function plot. 7 | 8 | % Define variables: 9 | % ii -- Index variable 10 | % jj -- Index variable 11 | % linespec -- String defining plot characteristics 12 | % msg -- Error message 13 | % varargin -- Cell array containing input arguments 14 | % x -- x values to plot 15 | % y -- y values to plot 16 | % 17 | % Record of revisions: 18 | % Date Programmer Description of change 19 | % ==== ========== ===================== 20 | % 03/05/07 S. J. Chapman Original code 21 | 22 | % Check for a legal number of input arguments. 23 | % We need at least 2 points to plot a line... 24 | msg = nargchk(2,Inf,nargin); 25 | error(msg); 26 | 27 | % Initialize values 28 | jj = 0; 29 | linespec = ''; 30 | 31 | % Get the x and y values, making sure to save the line 32 | % specification string, if one exists. 33 | for ii = 1:nargin 34 | 35 | % Is this argument an [x,y] pair or the line 36 | % specification? 37 | if ischar(varargin{ii}) 38 | 39 | % Save line specification 40 | linespec = varargin{ii}; 41 | 42 | else 43 | 44 | % This is an [x,y] pair. Recover the values. 45 | jj = jj + 1; 46 | x(jj) = varargin{ii}(1); 47 | y(jj) = varargin{ii}(2); 48 | 49 | end 50 | end 51 | 52 | % Plot function. 53 | if isempty(linespec) 54 | plot(x,y); 55 | else 56 | plot(x,y,linespec); 57 | end 58 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap7/simul.m: -------------------------------------------------------------------------------- 1 | % Script file: simul.m 2 | % 3 | % Purpose: 4 | % This program solves a system of 8 linear equations in 8 5 | % unknowns (a*x = b), using both full and sparse matrices. 6 | % 7 | % Record of revisions: 8 | % Date Programmer Description of change 9 | % ==== ========== ===================== 10 | % 03/03/07 S. J. Chapman Original code 11 | % 12 | % Define variables: 13 | % a -- Coefficients of x (full matrix) 14 | % as -- Coefficients of x (sparse matrix) 15 | % b -- Constant coefficients (full matrix) 16 | % bs -- Constant coefficients (sparse matrix) 17 | % x -- Solution (full matrix) 18 | % xs -- Solution (sparse matrix) 19 | 20 | % Define coefficients of the equation a*x = b for 21 | % the full matrix solution. 22 | a = [ 1.0 0.0 1.0 0.0 0.0 2.0 0.0 -1.0; ... 23 | 0.0 1.0 0.0 0.4 0.0 0.0 0.0 0.0; ... 24 | 0.5 0.0 2.0 0.0 0.0 0.0 -1.0 0.0; ... 25 | 0.0 0.0 0.0 2.0 0.0 1.0 0.0 0.0; ... 26 | 0.0 0.0 1.0 1.0 1.0 0.0 0.0 0.0; ... 27 | 0.0 0.0 0.0 1.0 0.0 1.0 0.0 0.0; ... 28 | 0.5 0.0 0.0 0.0 0.0 0.0 1.0 0.0; ... 29 | 0.0 1.0 0.0 0.0 0.0 0.0 0.0 1.0]; 30 | 31 | b = [ 3.0 2.0 -1.5 1.0 -2.0 1.0 1.0 1.0]'; 32 | 33 | % Define coefficients of the equation a*x = b for 34 | % the sparse matrix solution. 35 | as = sparse(a); 36 | bs = sparse(b); 37 | 38 | % Solve the system both ways 39 | disp ('Full matrix solution:'); 40 | x = a\b 41 | 42 | disp ('Sparse matrix solution:'); 43 | xs = as\bs 44 | 45 | % Show workspace 46 | disp('Workspace contents after the solutions:') 47 | whos 48 | 49 | 50 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap7/sparse1.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/101Hub/Matlab101/07273f68f1147a110443aeb121fa10962234f298/assets/《Matlab编程》源码/chap7/sparse1.mat -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap7/student.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/101Hub/Matlab101/07273f68f1147a110443aeb121fa10962234f298/assets/《Matlab编程》源码/chap7/student.mat -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap7/temp.m: -------------------------------------------------------------------------------- 1 | temp4 = fact(10) / (fact(4) * fact(10-4)) * 0.5^4 * (1-0.5)^(10-4) 2 | temp5 = fact(10) / (fact(5) * fact(10-5)) * 0.5^5 * (1-0.5)^(10-5) 3 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap7/test1.m: -------------------------------------------------------------------------------- 1 | function test1(varargin) 2 | disp(['There are ' int2str(nargin) ' arguments.']); 3 | disp('The input arguments are:'); 4 | disp(varargin) 5 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap7/test2.m: -------------------------------------------------------------------------------- 1 | function [nvals,varargout] = test2(mult) 2 | % nvals is the number of random values returned 3 | % varargout contains the random values returned 4 | nvals = nargout - 1; 5 | for ii = 1:nargout-1 6 | varargout{ii} = randn * mult; 7 | end 8 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap7/test_preallocate.m: -------------------------------------------------------------------------------- 1 | % Script file: test_preallocate.m 2 | % 3 | % Purpose: 4 | % This program tests the creation of cell arrays with and 5 | % without preallocation. 6 | % 7 | % Record of revisions: 8 | % Date Programmer Description of change 9 | % ==== ========== ===================== 10 | % 03/04/07 S. J. Chapman Original code 11 | % 12 | % Define variables: 13 | % a -- Cell array 14 | % maxvals -- Maximum values in cell array 15 | 16 | % Create array without preallocation 17 | clear all 18 | maxvals = 50000; 19 | tic 20 | for ii = 1:maxvals 21 | a{ii} = ['Element ' int2str(ii)]; 22 | end 23 | disp( ['Elapsed time without preallocation = ' num2str(toc)] ); 24 | 25 | % Create array with preallocation 26 | clear all 27 | maxvals = 50000; 28 | tic 29 | a = cell(1,maxvals); 30 | for ii = 1:maxvals 31 | a{ii} = ['Element ' int2str(ii)]; 32 | end 33 | disp( ['Elapsed time with preallocation = ' num2str(toc)] ); 34 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap8/binary_io.m: -------------------------------------------------------------------------------- 1 | % Script file: binary_io.m 2 | % 3 | % Purpose: 4 | % To illustrate the use of binary i/o functions. 5 | % 6 | % Record of revisions: 7 | % Date Programmer Description of change 8 | % ==== ========== ===================== 9 | % 03/21/07 S. J. Chapman Original code 10 | % 11 | % Define variables: 12 | % count -- Number of values read / written 13 | % fid -- File id 14 | % filename -- File name 15 | % in_array -- Input array 16 | % msg -- Open error message 17 | % out_array -- Output array 18 | % status -- Operation status 19 | 20 | % Prompt for file name 21 | filename = input('Enter file name: ','s'); 22 | 23 | % Generate the data array 24 | out_array = randn(1,10000); 25 | 26 | % Open the output file for writing. 27 | [fid,msg] = fopen(filename,'w'); 28 | 29 | % Was the open successful? 30 | if fid > 0 31 | 32 | % Write the output data. 33 | count = fwrite(fid,out_array,'float64'); 34 | 35 | % Tell user 36 | disp([int2str(count) ' values written...']); 37 | 38 | % Close the file 39 | status = fclose(fid); 40 | 41 | else 42 | 43 | % Output file open failed. Display message. 44 | disp(msg); 45 | 46 | end 47 | 48 | % Now try to recover the data. Open the 49 | % file for reading. 50 | [fid,msg] = fopen(filename,'r'); 51 | 52 | % Was the open successful? 53 | if fid > 0 54 | 55 | % Read the input data. 56 | [in_array, count] = fread(fid,[100 100],'float64'); 57 | 58 | % Tell user 59 | disp([int2str(count) ' values read...']); 60 | 61 | % Close the file 62 | status = fclose(fid); 63 | 64 | else 65 | 66 | % Input file open failed. Display message. 67 | disp(msg); 68 | 69 | end 70 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap8/compare.m: -------------------------------------------------------------------------------- 1 | % Script file: compare.m 2 | % 3 | % Purpose: 4 | % To compare binary and formatted I/O operations. 5 | % This program generates an array of 10,000 random 6 | % values and writes it to disk both as a binary and 7 | % as a formatted file. 8 | % 9 | % Record of revisions: 10 | % Date Programmer Description of change 11 | % ==== ========== ===================== 12 | % 03/22/07 S. J. Chapman Original code 13 | % 14 | % Define variables: 15 | % count -- Number of values read / written 16 | % fid -- File id 17 | % in_array -- Input array 18 | % msg -- Open error message 19 | % out_array -- Output array 20 | % status -- Operation status 21 | % time -- Elapsed time in seconds 22 | 23 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 24 | % Generate the data array. 25 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 26 | out_array = randn(1,10000); 27 | 28 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 29 | % First, time the binary output operation. 30 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 31 | % Reset timer 32 | tic; 33 | 34 | % Loop for 10 times 35 | for ii = 1:10 36 | 37 | % Open the binary output file for writing. 38 | [fid,msg] = fopen('unformatted.dat','w'); 39 | 40 | % Write the data 41 | count = fwrite(fid,out_array,'float64'); 42 | 43 | % Close the file 44 | status = fclose(fid); 45 | 46 | end 47 | 48 | % Get the average time 49 | time = toc / 10; 50 | fprintf ('Write time for unformatted file = %6.3f\n',time); 51 | 52 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 53 | % Next, time the formatted output operation. 54 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 55 | % Reset timer 56 | tic; 57 | 58 | % Loop for 10 times 59 | for ii = 1:10 60 | 61 | % Open the formatted output file for writing. 62 | [fid,msg] = fopen('formatted.dat','wt'); 63 | 64 | % Write the data 65 | count = fprintf(fid,'%23.15e\n',out_array); 66 | 67 | % Close the file 68 | status = fclose(fid); 69 | end 70 | 71 | % Get the average time 72 | time = toc / 10; 73 | fprintf ('Write time for formatted file = %6.3f\n',time); 74 | 75 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 76 | % Time the binary input operation. 77 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 78 | % Reset timer 79 | tic; 80 | 81 | % Loop for 10 times 82 | for ii = 1:10 83 | 84 | % Open the binary file for reading. 85 | [fid,msg] = fopen('unformatted.dat','r'); 86 | 87 | % Read the data 88 | [in_array, count] = fread(fid,Inf,'float64'); 89 | 90 | % Close the file 91 | status = fclose(fid); 92 | 93 | end 94 | 95 | % Get the average time 96 | time = toc / 10; 97 | fprintf ('Read time for unformatted file = %6.3f\n',time); 98 | 99 | 100 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 101 | % Time the formatted input operation. 102 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 103 | % Reset timer 104 | tic; 105 | 106 | % Loop for 10 times 107 | for ii = 1:10 108 | 109 | % Open the formatted file for reading. 110 | [fid,msg] = fopen('formatted.dat','rt'); 111 | 112 | % Read the data 113 | [in_array, count] = fscanf(fid,'%f',Inf); 114 | 115 | % Close the file 116 | status = fclose(fid); 117 | 118 | end 119 | 120 | % Get the average time 121 | time = toc / 10; 122 | fprintf ('Read time for formatted file = %6.3f\n',time); 123 | 124 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap8/input1: -------------------------------------------------------------------------------- 1 | 1.1 1.1 2 | 2.2 2.2 3 | 3.3 3.3 4 | 4.4 4.4 5 | 5.5 5.5 6 | 6.6 6.6 7 | 7.7 7.7 8 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap8/input2: -------------------------------------------------------------------------------- 1 | 1.1 1.01 2 | 2.2 2.30 3 | 3.3 3.05 4 | 4.4 4.28 5 | 5.5 5.75 6 | 6.6 6.48 7 | 7.7 7.84 8 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap8/int.dat: -------------------------------------------------------------------------------- 1 |  2 |  -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap8/lsqfit.m: -------------------------------------------------------------------------------- 1 | % Script file: lsqfit.m 2 | % 3 | % Purpose: 4 | % To perform a least-squares fit of an input data set 5 | % to a straight line, and print out the resulting slope 6 | % and intercept values. The input data for this fit 7 | % comes from a user-specified input data file. 8 | % 9 | % Record of revisions: 10 | % Date Programmer Description of change 11 | % ==== ========== ===================== 12 | % 03/24/07 S. J. Chapman Original code 13 | % 14 | % Define variables: 15 | % count -- number of values read 16 | % filename -- Input file name 17 | % fid -- File id 18 | % msg -- Open error message 19 | % n -- Number of input data pairs (x,y) 20 | % slope -- Slope of the line 21 | % sum_x -- Sum of all input X values 22 | % sum_x2 -- Sum of all input X values squared 23 | % sum_xy -- Sum of all input X*Y values 24 | % sum_y -- Sum of all input Y values 25 | % x -- An input X value 26 | % x_bar -- Average X value 27 | % y -- An input Y value 28 | % y_bar -- Average Y value 29 | % y_int -- Y-axis intercept of the line 30 | 31 | % Initialize sums 32 | n = 0; sum_x = 0; sum_y = 0; sum_x2 = 0; sum_xy = 0; 33 | 34 | % Prompt user and get the name of the input file. 35 | disp('This program performs a least-squares fit of an'); 36 | disp('input data set to a straight line. Enter the name'); 37 | disp('of the file containing the input (x,y) pairs: ' ); 38 | filename = input(' ','s'); 39 | 40 | % Open the input file 41 | [fid,msg] = fopen(filename,'rt'); 42 | 43 | % Check to see if the open failed. 44 | if fid < 0 45 | 46 | % There was an error--tell user. 47 | disp(msg); 48 | 49 | else 50 | 51 | % File opened successfully. Read the (x,y) pairs from 52 | % the input file. Get first (x,y) pair before the 53 | % loop starts. 54 | [in,count] = fscanf(fid,'%g %g',2); 55 | 56 | while ~feof(fid) 57 | x = in(1); 58 | y = in(2); 59 | n = n + 1; % 60 | sum_x = sum_x + x; % Calculate 61 | sum_y = sum_y + y; % statistics 62 | sum_x2 = sum_x2 + x.^2; % 63 | sum_xy = sum_xy + x * y; % 64 | 65 | % Get next (x,y) pair 66 | [in,count] = fscanf(fid,'%f',[1 2]); 67 | 68 | end 69 | 70 | % Close the file 71 | fclose(fid); 72 | 73 | % Now calculate the slope and intercept. 74 | x_bar = sum_x / n; 75 | y_bar = sum_y / n; 76 | slope = (sum_xy - sum_x*y_bar) / (sum_x2 - sum_x*x_bar); 77 | y_int = y_bar - slope * x_bar; 78 | 79 | % Tell user. 80 | fprintf('Regression coefficients for the least-squares line:\n'); 81 | fprintf(' Slope (m) = %12.3f\n',slope); 82 | fprintf(' Intercept (b) = %12.3f\n',y_int); 83 | fprintf(' No of points = %12d\n',n); 84 | 85 | end 86 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap8/lsqfit_plot.m: -------------------------------------------------------------------------------- 1 | % Script file: lsqfit.m 2 | % 3 | % Purpose: 4 | % To perform a least-squares fit of an input data set 5 | % to a straight line, and print out the resulting slope 6 | % and intercept values. The input data for this fit 7 | % comes from a user-specified input data file. 8 | % 9 | % Record of revisions: 10 | % Date Programmer Description of change 11 | % ==== ========== ===================== 12 | % 03/24/07 S. J. Chapman Original code 13 | % 14 | % Define variables: 15 | % count -- number of values read 16 | % filename -- Input file name 17 | % fid -- File id 18 | % msg -- Open error message 19 | % n -- Number of input data pairs (x,y) 20 | % slope -- Slope of the line 21 | % sum_x -- Sum of all input X values 22 | % sum_x2 -- Sum of all input X values squared 23 | % sum_xy -- Sum of all input X*Y values 24 | % sum_y -- Sum of all input Y values 25 | % x -- An input X value 26 | % x_bar -- Average X value 27 | % y -- An input Y value 28 | % y_bar -- Average Y value 29 | % y_int -- Y-axis intercept of the line 30 | 31 | % Initialize sums 32 | n = 0; sum_x = 0; sum_y = 0; sum_x2 = 0; sum_xy = 0; 33 | x1 = []; y1 = []; 34 | 35 | % Prompt user and get the name of the input file. 36 | disp('This program performs a least-squares fit of an'); 37 | disp('input data set to a straight line. Enter the name'); 38 | disp('of the file containing the input (x,y) pairs: ' ); 39 | filename = input(' ','s'); 40 | 41 | % Open the input file 42 | [fid,msg] = fopen(filename,'rt'); 43 | 44 | % Check to see if the open failed. 45 | if fid < 0 46 | 47 | % There was an error--tell user. 48 | disp(msg); 49 | 50 | else 51 | 52 | % File opened successfully. Read the (x,y) pairs from 53 | % the input file. Get first (x,y) pair before the 54 | % loop starts. 55 | [in,count] = fscanf(fid,'%g %g',2); 56 | 57 | while ~feof(fid) 58 | x = in(1); 59 | y = in(2); 60 | n = n + 1; % 61 | sum_x = sum_x + x; % Calculate 62 | sum_y = sum_y + y; % statistics 63 | sum_x2 = sum_x2 + x.^2; % 64 | sum_xy = sum_xy + x * y; % 65 | x1(n) = x; 66 | y1(n) = y; 67 | 68 | % Get next (x,y) pair 69 | [in,count] = fscanf(fid,'%f',[1 2]); 70 | 71 | end 72 | 73 | % Close the file 74 | fclose(fid); 75 | 76 | % Now calculate the slope and intercept. 77 | x_bar = sum_x / n; 78 | y_bar = sum_y / n; 79 | slope = (sum_xy - sum_x*y_bar) / (sum_x2 - sum_x*x_bar); 80 | y_int = y_bar - slope * x_bar; 81 | 82 | % Tell user. 83 | fprintf('Regression coefficients for the least-squares line:\n'); 84 | fprintf(' Slope (m) = %12.3f\n',slope); 85 | fprintf(' Intercept (b) = %12.3f\n',y_int); 86 | fprintf(' No of points = %12d\n',n); 87 | 88 | % Now plot this data set 89 | figure(1); 90 | plot(x1,y1,'bo','LineWidth',2); 91 | hold on; 92 | 93 | % Now plot the fitted line 94 | x2 = x1; 95 | y2 = slope .* x2 + y_int; 96 | plot(x2,y2,'r-','LineWidth',2); 97 | hold off; 98 | 99 | % Title and labels 100 | title ('\bfLeast-Squares Fit'); 101 | xlabel ('\bf\itx'); 102 | ylabel ('\bf\ity'); 103 | grid on; 104 | 105 | end 106 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap8/output.m: -------------------------------------------------------------------------------- 1 | % Script file: output.m 2 | % 3 | % Purpose: 4 | % To demonstrate opening an output file properly. 5 | % This program checks for the existence of an output 6 | % file. If it exists, the program checks to see if 7 | % the old file should be deleted, or if the new data 8 | % should be appended to the old file. 9 | % 10 | % Record of revisions: 11 | % Date Programmer Description of change 12 | % ==== ========== ===================== 13 | % 03/24/07 S. J. Chapman Original code 14 | % 15 | % Define variables: 16 | % fid -- File id 17 | % out_filename -- Output file name 18 | % yn -- Yes/No response 19 | 20 | % Get the output file name. 21 | out_filename = input('Enter output filename: ','s'); 22 | 23 | % Check to see if the file exists. 24 | if exist(out_filename,'file') 25 | 26 | % The file exists 27 | disp('Output file already exists.'); 28 | yn = input('Keep existing file? (y/n) ','s'); 29 | 30 | if yn == 'n' 31 | fid = fopen(out_filename,'wt'); 32 | else 33 | fid = fopen(out_filename,'at'); 34 | end 35 | 36 | else 37 | 38 | % File doesn't exist 39 | fid = fopen(out_filename,'wt'); 40 | 41 | end 42 | 43 | % Output data 44 | fprintf(fid,'%s\n',date); 45 | 46 | % Close file 47 | fclose(fid); 48 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap8/sin_cos_table.m: -------------------------------------------------------------------------------- 1 | fprintf(' Table of Cosines and Sines\n\n'); 2 | fprintf(' theta cos(theta) sin(theta)\n'); 3 | fprintf(' ===== ========== ==========\n'); 4 | for ii = 0:0.1:1 5 | theta = pi * ii; 6 | fprintf('%7.4f %11.5f %11.5f\n', theta, cos(theta), sin(theta)); 7 | end -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap8/table.m: -------------------------------------------------------------------------------- 1 | % Script file: table.m 2 | % 3 | % Purpose: 4 | % To create a table of square roots, squares, and 5 | % cubes. 6 | % 7 | % Record of revisions: 8 | % Date Programmer Description of change 9 | % ==== ========== ===================== 10 | % 03/22/07 S. J. Chapman Original code 11 | % 12 | % Define variables: 13 | % cube -- Cubes 14 | % ii -- Index variable 15 | % square -- Squares 16 | % square_roots -- Square roots 17 | % out -- Output array 18 | 19 | % Print the title of the table. 20 | fprintf(' Table of Square Roots, Squares, and Cubes\n\n'); 21 | 22 | % Print column headings 23 | fprintf(' Number Square Root Square Cube\n'); 24 | fprintf(' ====== =========== ====== ====\n'); 25 | 26 | % Generate the required data 27 | ii = 1:10; 28 | square_root = sqrt(ii); 29 | square = ii.^2; 30 | cube = ii.^3; 31 | 32 | % Create the output array 33 | out = [ii' square_root' square' cube']; 34 | 35 | % Print the data 36 | for ii = 1:10 37 | fprintf (' %2d %11.4f %6d %8d\n',out(ii,:)); 38 | end 39 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap8/test_input.dat: -------------------------------------------------------------------------------- 1 | James Jones O+ 3.51 22 Yes 2 | Sally Smith A+ 3.28 23 No 3 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap8/test_input1.dat: -------------------------------------------------------------------------------- 1 | James Jones O+ 3.51 22 Yes 2 | Sally Smith A+ 3.28 23 No 3 | Hans Carter B- 2.84 19 Yes 4 | Sam Spade A+ 3.12 21 Yes 5 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap8/test_matfile.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/101Hub/Matlab101/07273f68f1147a110443aeb121fa10962234f298/assets/《Matlab编程》源码/chap8/test_matfile.mat -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap8/testfile: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/101Hub/Matlab101/07273f68f1147a110443aeb121fa10962234f298/assets/《Matlab编程》源码/chap8/testfile -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap8/text grades.xls: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/101Hub/Matlab101/07273f68f1147a110443aeb121fa10962234f298/assets/《Matlab编程》源码/chap8/text grades.xls -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap8/unformatted.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/101Hub/Matlab101/07273f68f1147a110443aeb121fa10962234f298/assets/《Matlab编程》源码/chap8/unformatted.dat -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap8/workspace.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/101Hub/Matlab101/07273f68f1147a110443aeb121fa10962234f298/assets/《Matlab编程》源码/chap8/workspace.mat -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap9/fig10_1.fig: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/101Hub/Matlab101/07273f68f1147a110443aeb121fa10962234f298/assets/《Matlab编程》源码/chap9/fig10_1.fig -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap9/fig10_1.m: -------------------------------------------------------------------------------- 1 | function varargout = fig10_1(varargin) 2 | % FIG10_1 Application M-file for fig10_1.fig 3 | % FIG = FIG10_1 launch fig10_1 GUI. 4 | % FIG10_1('callback_name', ...) invoke the named callback. 5 | 6 | % Last Modified by GUIDE v2.5 07-Mar-2004 20:46:39 7 | 8 | if nargin == 0 % LAUNCH GUI 9 | 10 | fig = openfig(mfilename,'reuse'); 11 | 12 | % Generate a structure of handles to pass to callbacks, and store it. 13 | handles = guihandles(fig); 14 | guidata(fig, handles); 15 | 16 | if nargout > 0 17 | varargout{1} = fig; 18 | end 19 | 20 | elseif ischar(varargin{1}) % INVOKE NAMED SUBFUNCTION OR CALLBACK 21 | 22 | try 23 | [varargout{1:nargout}] = feval(varargin{:}); % FEVAL switchyard 24 | catch 25 | disp(lasterr); 26 | end 27 | 28 | end 29 | 30 | 31 | %| ABOUT CALLBACKS: 32 | %| GUIDE automatically appends subfunction prototypes to this file, and 33 | %| sets objects' callback properties to call them through the FEVAL 34 | %| switchyard above. This comment describes that mechanism. 35 | %| 36 | %| Each callback subfunction declaration has the following form: 37 | %| (H, EVENTDATA, HANDLES, VARARGIN) 38 | %| 39 | %| The subfunction name is composed using the object's Tag and the 40 | %| callback type separated by '_', e.g. 'slider2_Callback', 41 | %| 'figure1_CloseRequestFcn', 'axis1_ButtondownFcn'. 42 | %| 43 | %| H is the callback object's handle (obtained using GCBO). 44 | %| 45 | %| EVENTDATA is empty, but reserved for future use. 46 | %| 47 | %| HANDLES is a structure containing handles of components in GUI using 48 | %| tags as fieldnames, e.g. handles.figure1, handles.slider2. This 49 | %| structure is created at GUI startup using GUIHANDLES and stored in 50 | %| the figure's application data using GUIDATA. A copy of the structure 51 | %| is passed to each callback. You can store additional information in 52 | %| this structure at GUI startup, and you can change the structure 53 | %| during callbacks. Call guidata(h, handles) after changing your 54 | %| copy to replace the stored original so that subsequent callbacks see 55 | %| the updates. Type "help guihandles" and "help guidata" for more 56 | %| information. 57 | %| 58 | %| VARARGIN contains any extra arguments you have passed to the 59 | %| callback. Specify the extra arguments by editing the callback 60 | %| property in the inspector. By default, GUIDE sets the property to: 61 | %| ('', gcbo, [], guidata(gcbo)) 62 | %| Add any extra arguments after the last argument, before the final 63 | %| closing parenthesis. 64 | 65 | 66 | 67 | % -------------------------------------------------------------------- 68 | function varargout = pushbutton1_Callback(h, eventdata, handles, varargin) 69 | % Stub for Callback of the uicontrol handles.pushbutton1. 70 | disp('pushbutton1 Callback not implemented yet.') 71 | 72 | 73 | % -------------------------------------------------------------------- 74 | function varargout = togglebutton1_Callback(h, eventdata, handles, varargin) 75 | % Stub for Callback of the uicontrol handles.togglebutton1. 76 | disp('togglebutton1 Callback not implemented yet.') 77 | 78 | 79 | % -------------------------------------------------------------------- 80 | function varargout = radiobutton1_Callback(h, eventdata, handles, varargin) 81 | % Stub for Callback of the uicontrol handles.radiobutton1. 82 | disp('radiobutton1 Callback not implemented yet.') 83 | 84 | 85 | % -------------------------------------------------------------------- 86 | function varargout = checkbox1_Callback(h, eventdata, handles, varargin) 87 | % Stub for Callback of the uicontrol handles.checkbox1. 88 | disp('checkbox1 Callback not implemented yet.') 89 | 90 | 91 | % -------------------------------------------------------------------- 92 | function varargout = edit1_Callback(h, eventdata, handles, varargin) 93 | % Stub for Callback of the uicontrol handles.edit1. 94 | disp('edit1 Callback not implemented yet.') 95 | 96 | 97 | % -------------------------------------------------------------------- 98 | function varargout = slider1_Callback(h, eventdata, handles, varargin) 99 | % Stub for Callback of the uicontrol handles.slider1. 100 | disp('slider1 Callback not implemented yet.') 101 | 102 | 103 | % -------------------------------------------------------------------- 104 | function varargout = listbox1_Callback(h, eventdata, handles, varargin) 105 | % Stub for Callback of the uicontrol handles.listbox1. 106 | disp('listbox1 Callback not implemented yet.') 107 | 108 | 109 | % -------------------------------------------------------------------- 110 | function varargout = radiobutton3_Callback(h, eventdata, handles, varargin) 111 | % Stub for Callback of the uicontrol handles.radiobutton3. 112 | disp('radiobutton3 Callback not implemented yet.') 113 | 114 | 115 | % --- Executes on button press in radiobutton2. 116 | function radiobutton2_Callback(hObject, eventdata, handles) 117 | % hObject handle to radiobutton2 (see GCBO) 118 | % eventdata reserved - to be defined in a future version of MATLAB 119 | % handles structure with handles and user data (see GUIDATA) 120 | 121 | % Hint: get(hObject,'Value') returns toggle state of radiobutton2 122 | 123 | 124 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap9/plotsinc.m: -------------------------------------------------------------------------------- 1 | % Script file: plotsinc.m 2 | % 3 | % Purpose: 4 | % This program illustrates the use of handle graphics 5 | % commands by creating a plot of sinc(x) from -3*pi to 6 | % 3*pi, and modifying the characteristics of the figure, 7 | % axes, and line using the "set" function. 8 | % 9 | % Record of revisions: 10 | % Date Programmer Description of change 11 | % ==== ========== ===================== 12 | % 04/02/07 S. J. Chapman Original code 13 | % 14 | % Define variables: 15 | % hndl -- Handle of line 16 | % x -- Independent variable 17 | % y -- sinc(x) 18 | 19 | % Calculate sinc(x) 20 | x = -3*pi:pi/10:3*pi; 21 | y = sin(x) ./ x; 22 | 23 | % Find the zero value and fix it up. The zero is 24 | % located in the middle of the x array. 25 | index = fix(length(y)/2) + 1; 26 | y(index) = 1; 27 | 28 | % Plot the function. 29 | hndl = plot(x,y); 30 | 31 | % Now modify the figure to create a pink background, 32 | % modify the axis to turn on y-axis grid lines, and 33 | % modify the line to be a 2-point wide orange line. 34 | set(gcf,'Color',[1 0.8 0.8]); 35 | set(gca,'YGrid','on'); 36 | set(hndl,'Color',[1 0.5 0],'LineWidth',3); 37 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap9/position_object.m: -------------------------------------------------------------------------------- 1 | % Script file: position_object.m 2 | % 3 | % Purpose: 4 | % This program illustrates the positioning of graphics 5 | % graphics objects. It creates a figure, and then places 6 | % two overlapping sets of axes on the figure. The first 7 | % set of axes is placed in the lower left hand corner of 8 | % the figure, and contains a plot of sin(x). The second 9 | % set of axes is placed in the upper right hand corner of 10 | % the figure, and contains a plot of cos(x). Then two 11 | % text strings are added to the axes, illustrating the 12 | % positioning of text within axes. 13 | % 14 | % Record of revisions: 15 | % Date Programmer Description of change 16 | % ==== ========== ===================== 17 | % 04/03/07 S. J. Chapman Original code 18 | % 19 | % Define variables: 20 | % h1 -- Handle of sine line 21 | % h2 -- Handle of cosine line 22 | % ha1 -- Handle of first axes 23 | % ha2 -- Handle of second axes 24 | % x -- Independent variable 25 | % y1 -- sin(x) 26 | % y2 -- cos(x) 27 | 28 | % Calculate sin(x) and cos(x) 29 | x = -2*pi:pi/10:2*pi; 30 | y1 = sin(x); 31 | y2 = cos(x); 32 | 33 | % Create a new figure 34 | figure; 35 | 36 | % Create the first set of axes and plot sin(x). 37 | % Note that the position of the axes is expressed 38 | % in normalized units. 39 | ha1 = axes('Position',[.05 .05 .5 .5]); 40 | h1 = plot(x,y1); 41 | set(h1,'LineWidth',2); 42 | title('\bfPlot of sin \itx'); 43 | xlabel('\bf\itx'); 44 | ylabel('\bfsin \itx'); 45 | axis([-8 8 -1 1]); 46 | 47 | % Create the second set of axes and plot cos(x). 48 | % Note that the position of the axes is expressed 49 | % in normalized units. 50 | ha2 = axes('Position',[.45 .45 .5 .5]); 51 | h2 = plot(x,y1); 52 | set(h2,'LineWidth',2,'Color','r','LineStyle','--'); 53 | title('\bfPlot of cos \itx'); 54 | xlabel('\bf\itx'); 55 | ylabel('\bfsin \itx'); 56 | axis([-8 8 -1 1]); 57 | 58 | % Create a text string attached to the line on the first 59 | % set of axes. 60 | axes(ha1); 61 | text(-pi,0.0,'sin(x)\rightarrow','HorizontalAlignment','right'); 62 | 63 | % Create a text string in the lower left hand corner 64 | % of the second set of axes. 65 | axes(ha2); 66 | text(-7.5,-0.9,'Test string 2'); 67 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap9/select_object.m: -------------------------------------------------------------------------------- 1 | % Script file: select_object.m 2 | % 3 | % Purpose: 4 | % This program illustrates the use of waitforbuttonpress 5 | % and gco to select graphics objects. It creates a plot 6 | % of sin(x) and cos(x), and then allows a user to select 7 | % any object and examine its properties. The program 8 | % terminates when a key press occurs. 9 | % 10 | % Record of revisions: 11 | % Date Programmer Description of change 12 | % ==== ========== ===================== 13 | % 04/02/07 S. J. Chapman Original code 14 | % 15 | % Define variables: 16 | % details -- Object details 17 | % h1 -- handle of sine line 18 | % h2 -- handle of cosine line 19 | % handle -- handle of current object 20 | % k -- Result of waitforbuttonpress 21 | % type -- Object type 22 | % x -- Independent variable 23 | % y1 -- sin(x) 24 | % y2 -- cos(x) 25 | % yn -- Yes/No 26 | 27 | % Calculate sin(x) and cos(x) 28 | x = -3*pi:pi/10:3*pi; 29 | y1 = sin(x); 30 | y2 = cos(x); 31 | 32 | % Plot the functions. 33 | h1 = plot(x,y1); 34 | set(h1,'LineWidth',2); 35 | hold on; 36 | h2 = plot(x,y2); 37 | set(h2,'LineWidth',2,'LineStyle',':','Color','r'); 38 | title('\bfPlot of sin \itx \rm\bf and cos \itx'); 39 | xlabel('\bf\itx'); 40 | ylabel('\bfsin \itx \rm\bf and cos \itx'); 41 | legend('sine','cosine'); 42 | hold off; 43 | 44 | % Now set up a loop and wait for a mouse click. 45 | k = waitforbuttonpress; 46 | 47 | while k == 0 48 | 49 | % Get the handle of the object 50 | handle = gco; 51 | 52 | % Get the type of this object. 53 | type = get(handle,'Type'); 54 | 55 | % Display object type 56 | disp (['Object type = ' type '.']); 57 | 58 | % Do we display the details? 59 | yn = input('Do you want to display details? (y/n) ','s'); 60 | 61 | if yn == 'y' 62 | details = get(handle); 63 | disp(details); 64 | end 65 | 66 | % Check for another mouse click 67 | k = waitforbuttonpress; 68 | end 69 | -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/chap9/test1.fig: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/101Hub/Matlab101/07273f68f1147a110443aeb121fa10962234f298/assets/《Matlab编程》源码/chap9/test1.fig -------------------------------------------------------------------------------- /assets/《Matlab编程》源码/temp_conversion.m: -------------------------------------------------------------------------------- 1 | % Script file: temp_conversion.m 2 | 3 | % Purpose: 4 | % To convert an input temperature from degrees Fahrenheit to 5 | % an output temperature in kelvins. 6 | 7 | 8 | % Record of revisions: 9 | % Date Programmer Description of change 10 | % ==== ========================= 11 | % 12/01/97 S.J.Chapman Original code 12 | 13 | % Define variables: 14 | % temp_f --Temperature in degrees Fahrenheit 15 | % temp_k --Temperature in kelvins 16 | 17 | %Prompt the user for the input temperature. 18 | temp_f=input('Enter the temperature in degrees Fahrenheit:'); 19 | %Converttokelvins. 20 | temp_k=(5/9)*(temp_f-32)+273.15; 21 | %Writeouttheresult. 22 | fprintf('%6.2f degrees Fahrenheit = %6.2f kelvins.\n',... 23 | temp_f,temp_k); -------------------------------------------------------------------------------- /assets/内置函数: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/101Hub/Matlab101/07273f68f1147a110443aeb121fa10962234f298/assets/内置函数 -------------------------------------------------------------------------------- /assets/图形对象: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/101Hub/Matlab101/07273f68f1147a110443aeb121fa10962234f298/assets/图形对象 -------------------------------------------------------------------------------- /assets/图形对象层次结构: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/101Hub/Matlab101/07273f68f1147a110443aeb121fa10962234f298/assets/图形对象层次结构 -------------------------------------------------------------------------------- /assets/捷径表达式: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/101Hub/Matlab101/07273f68f1147a110443aeb121fa10962234f298/assets/捷径表达式 -------------------------------------------------------------------------------- /docs/Effective_Matlab.md: -------------------------------------------------------------------------------- 1 | ## 《Effective Matlab 编程笔记》 2 | 3 | > 序: 4 | > Effective Matlab—编写高效的 Matlab 代码,记录了本人在学习使用 matlab 过程中的一些思考、技巧、代码规范及其它经验,集中于高效使用 Matlab 这一主题,目的是为了编写出简洁、高效的 matlab 代码。 5 | 6 | ## 📑目录 7 | 8 | #### 第一部分 Matlab 基础 9 | 10 | * [Matlab 技巧、快捷键及注意事项](/docs/matlab技巧_快捷键_注意事项.md) 11 | * [Matlab 绘图技巧及经验总结](/Matlab绘图技巧及经验总结.md) 12 | 13 | #### 第二部分 Matlab 脚本和函数编程 14 | 15 | * [Matlab 编程规范\(Matlab Coding Style\)](/docs/Matlab编程规范.md) 16 | 17 | * [Matlab 最佳实践(Best Practice of Matlab)](/Matlab最佳实践.md) 18 | 19 | * [Matlab 的高级功能及进阶使用](/docs/Matlab的高级功能及进阶使用.md) 20 | 21 | * [Matlab 向量化编程\(Vectorization\)](#) 22 | 23 | * [Matlab 编程珠玑](Matlab编程珠玑.md) 24 | 25 | ## 🔎参考资料 26 | 27 | * [Matlab tips and tricks](http://www.ee.columbia.edu/~marios/matlab/matlab_tricks.html) 28 | * [《Matlab 编程》第二版 Stephen J. Chapman 著 邢树军 郑碧波译](/assets/Matlab编程%28第二版%29.pdf) 🗃[源代码](/assets/《Matlab编程》源码) 29 | * [《Matlab Guidelines》\(Richard K. Johnson\)](http://datatool.com/resources.html) 30 | * [《The Elements of MATLAB Style 》\(Richard K. Johnson\)](#) 31 | 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /docs/Matlab最佳实践.md: -------------------------------------------------------------------------------- 1 | ### 🏆Matlab 最佳实践(Best Practice of Matlab) 2 | 3 | > 序: 4 | > 5 | > Matlab 最佳实践整理收集了本人在学习、使用Matlab过程中优秀的Matlab使用案例,包含代码片段、设计模式、组织架构及其它高效的工具。 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /docs/Matlab的高级功能及进阶使用.md: -------------------------------------------------------------------------------- 1 | ### 🏹Matlab的高级功能及进阶使用 2 | 3 | #### Matlab **编辑器的进阶使用** 4 | 5 | * [代码节](#代码节) 6 | * [实时脚本和函数](#实时脚本和函数) 7 | * [代码编辑器高级功能](#代码编辑器高级功能) 8 | 9 | ##### 代码节 10 | 11 | MATLAB®文件通常包含多个命令。您通常一次仅专注于程序中的某个部分,分块操作代码。同样,向其他人解释您的文件时,通常分块介绍您的程序。为帮助介绍这些过程,使用_代码节_,也称为代码单元或单元模式。代码节包含 MATLAB 脚本中您想要作为一组求值的连续代码行,以两个注释字符 \(`%%`\) 开头。 12 | 13 | 要显式定义代码节界限,使用以下方法插入分节符: 14 | 15 | * 在**编辑器**选项卡上的**编辑**部分中,点击**插入**按钮组中的![](https://ww2.mathworks.cn/help/matlab/matlab_prog/new_section_ts_16_zh_CN.png)。 16 | 17 | * 在您想要开始新的代码节的行首输入两个百分比符号 \(`%%`\)。 18 | 19 | 与`%%`位于同一行中的文本称为_节标题_。可以选择是否包含节标题,不过,节标题可提高文件的可读性并在您发布代码时显示为题头。 20 | 21 | 参考:[https://ww2.mathworks.cn/help/matlab/matlab\_prog/run-sections-of-programs.html](https://ww2.mathworks.cn/help/matlab/matlab_prog/run-sections-of-programs.html) 22 | 23 | ##### 实时脚本和函数 24 | 25 | 包含嵌入式输出和格式化文本的实时编辑器程序文件 26 | 27 | 实时脚本和实时函数是用于与一系列 MATLAB®命令进行交互的程序文件。实时脚本包含输出和图形以及生成这些输出和图形的代码,它们共同显示在一个称为实时编辑器的交互式环境中。实时函数提供额外的灵活性,您可以通过它传递输入值并返回输出值。您可以在实时脚本和实时函数中添加格式化文本、图像、超链接和方程,以生成可与他人共享的交互式记叙脚本。 28 | 29 | 参考:[https://ww2.mathworks.cn/help/matlab/live-scripts-and-functions.html](https://ww2.mathworks.cn/help/matlab/live-scripts-and-functions.html) 30 | 31 | ##### 代码编辑器高级功能 32 | 33 | 使用 MATLAB®编辑器编辑程序 34 | 35 | * [ ] [**在编辑器中打开和保存文件**](https://ww2.mathworks.cn/help/matlab/matlab_prog/open-and-save-files.html) 36 | 37 | 使用多种不同的方法在编辑器中打开和保存文件。 38 | 39 | * [ ] [**在键入时检查语法**](https://ww2.mathworks.cn/help/matlab/matlab_env/check-syntax-as-you-type.html) 40 | 41 | 某些条目在命令行窗口和编辑器中以不同颜色显示,以帮助您识别 MATLAB 元素。这称为_语法高亮显示_。 42 | 43 | * [ ] [**检查代码中的错误和警告**](https://ww2.mathworks.cn/help/matlab/matlab_prog/check-code-for-errors-and-warnings.html) 44 | 45 | 使用 MATLAB 代码分析器可自动检查您的代码有无编码问题。 46 | 47 | * [ ] [**MATLAB 代码分析器报告**](https://ww2.mathworks.cn/help/matlab/matlab_prog/matlab-code-analyzer-report.html) 48 | 49 | 生成用于标识代码中的潜在错误、问题以及改进机会的报告。 50 | 51 | * [ ] [**提高代码可读性**](https://ww2.mathworks.cn/help/matlab/matlab_prog/improve-code-readability.html) 52 | 53 | 使用缩进、文本宽度指示和代码折叠以使您的代码更便于阅读。 54 | 55 | * [ ] [**在文件中查找并替换文本**](https://ww2.mathworks.cn/help/matlab/matlab_prog/find-and-replace-text-in-files.html) 56 | 57 | 在当前文件或多个文件中查找并替换文本。自动重命名函数或变量。 58 | 59 | * [ ] [**转到文件中的位置**](https://ww2.mathworks.cn/help/matlab/matlab_prog/file-navigation.html) 60 | 61 | 转到文件中的特定位置(例如行号或函数定义)、设置书签、在文件中向后和向前导航,以及从文件中打开文件或变量。 62 | 63 | * [ ] [**同时显示文件的两部分**](https://ww2.mathworks.cn/help/matlab/matlab_prog/display-two-parts-of-a-file-simultaneously.html) 64 | 65 | 您可以在编辑器中通过分割屏幕来同时显示某一文件的两个不同部分。利用此功能可轻松比较文件中的不同行或从文件的一个部分复制并粘贴到另一个部分。 66 | 67 | * [ ] [**在文件中添加提醒**](https://ww2.mathworks.cn/help/matlab/matlab_prog/add-reminders-to-files.html) 68 | 69 | 通过为文件添加注释可以更轻松地查找您的代码中您打算在日后改进、完成或更新的区域。 70 | 71 | * [ ] [**编辑器/调试器预设项**](https://ww2.mathworks.cn/help/matlab/matlab_env/about-editor-debugger-preferences.html) 72 | 73 | 使用“编辑器/调试器”预设项对编辑器进行自定义。 74 | 75 | * [ ] [**代码分析器预设项**](https://ww2.mathworks.cn/help/matlab/matlab_env/about-code-analyzer-preferences.html) 76 | 77 | 设置代码分析器预设项可启用或禁用代码分析器功能并指定消息指示标记预设项。 78 | 79 | -------------------------------------------------------------------------------- /docs/Matlab科学计算.md: -------------------------------------------------------------------------------- 1 | ## 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /docs/Matlab绘图技巧及经验总结.md: -------------------------------------------------------------------------------- 1 | ### 🖌Matlab绘图技巧及经验总结 2 | 3 | * [基础绘图命令](#基础绘图命令) 4 | 5 | * [图形对象属性控制绘图参数:set/get](#图形对象属性控制绘图参数:setget) 6 | 7 | #### 基础绘图命令 8 | 9 | ``` 10 | figure(1) 11 | x = linspace(0,2*pi,100); 12 | y = sin(x); 13 | y2 = cos(x); 14 | 15 | %%指定坐标轴范围 16 | 17 | xlim([xmin xmax]); 18 | ylim([ymin ymax]); 19 | zlim([zmin zmax]); 20 | 21 | axis([xmin xmax ymin ymax zmin zmax]) 22 | 23 | %% 绘制子图 24 | subplot(2,2,1) 25 | x = linspace(0,10); 26 | y1 = sin(x); 27 | plot(x,y1) 28 | title('Subplot 1: sin(x)') 29 | 30 | subplot(2,2,2) 31 | y2 = sin(2*x); 32 | plot(x,y2) 33 | title('Subplot 2: sin(2x)') 34 | 35 | subplot(2,2,[3 4]) 36 | y3 = sin(3*x); 37 | plot(x,y3) 38 | title('Subplot 3/4: sin(3x)') 39 | 40 | 41 | %%画在同一个figure中 42 | 43 | % 方法一 44 | plot(x,y,'--or') %LineSpec:线型、标记、颜色 45 | hold on 46 | plot(x,y2) 47 | hold off 48 | 49 | %方法二 50 | plot(x,y1,'LineSpec1',x,y2,'LineSpec2') 51 | 52 | 53 | grid on; %加网格线 54 | box on; %加坐标边框 55 | axis on/off; 56 | axis equal;%坐标轴采用等刻度 57 | 58 | %% title,legend,text,xlabel,ylabel的用法 59 | title(['Temperature is ',num2str(c),' C'])%包含变量c 60 | title({'First line';'Second line'})%分行 61 | legend({'Line 1','Line 2','Line 3','Line 4'},'FontSize',12,'TextColor','blue') 62 | ``` 63 | 64 | 参考: 65 | 66 | > [1.MATLAB绘图类型](https://ww2.mathworks.cn/help/matlab/creating_plots/types-of-matlab-plots.html) 67 | 68 | #### 图形对象属性控制绘图参数:set/get 69 | 70 | ![](/assets/图形对象层次结构) 71 | 72 | ``` 73 | h1=figure(1)% 74 | set(gcf,'Name','Property')%设置当前图窗的属性 75 | set(h1,'Name','Property')%设置当前图窗的属性 76 | 77 | a1=axes() 78 | set(gca,'Name','Property')%设置当前坐标轴的属性 79 | set(a1,'Name','Property')%设置当前坐标轴的属性 80 | 81 | 82 | p1 = plot(1:10,1:10); 83 | p1.LineWidth = 3; 84 | %或者 85 | set(p1,'Name','Property') 86 | 87 | t1=text(''); 88 | set(p1,'Name','Property'); 89 | ``` 90 | 91 | > [1.查看图形对象的属性及标识](https://ww2.mathworks.cn/help/matlab/graphics-object-properties.html) 92 | 93 | 94 | 95 | -------------------------------------------------------------------------------- /docs/Matlab编程珠玑.md: -------------------------------------------------------------------------------- 1 | ### ✨Matlab 编程珠玑 2 | 3 | 目录: 4 | 5 | #### [编程方法](#编程方法) 6 | 7 | * [自上而下的编程方法](#自上而下的编程方法) 8 | 9 | #### [优秀的 Matlab Codes](#优秀的-matlab-codes) 10 | 11 | * [变量的初始化](#变量的初始化) 12 | * 捷径表达式 13 | * 内置函数 14 | * 其它函数 15 | * 符号函数、内联函数及匿名函数 16 | * 执行字符串包含的matlab表达式:eval函数 17 | 18 | #### 编程方法 19 | 20 | ##### 自上而下的编程方法 21 | 22 | 1.清晰地陈述出你要解决的问题。 23 | 24 | 2.确定程序所需地输入量和程序所产生的输出量。 25 | 26 | 3.为你的程序设计算法 27 | 28 | 4.将算法转化为伪代码→MATLAB 语句 29 | 30 | 5.调试MATLAB 程序 31 | 32 | ![](/assets/matlab自上而下编程方法.png) 33 | 34 | 函数式编程思想\(functional programming\) 35 | 36 | ![](/assets/matlab调试2.png) 37 | 38 | #### 优秀的 Matlab Codes 39 | 40 | ##### 1.变量的初始化 41 | 42 | 三种方式初始化MATLAB 中的变 43 | 量: 44 | 45 | 1.用赋值语句初始化变量 46 | 47 | 2.用 input 函数从键盘输入初始化变量 48 | 49 | 3.从文件读取一个数据 50 | 51 | ###### 捷径表达式 52 | 53 | ``` 54 | first:incr:last 55 | ``` 56 | 57 | ![](/assets/捷径表达式) 58 | 59 | ###### 内置函数 60 | 61 | ![](/assets/内置函数) 62 | 63 | ###### 其它函数 64 | 65 | 1.deal函数 66 | 67 | ``` 68 | % 假如要初始化A,B,C和D为3*4阶矩阵,平常我们会用如下代码 69 | A = zeros(3,4); 70 | B = zeros(3,4); 71 | C = zeros(3,4); 72 | D = zeros(3,4); 73 | 74 | % 利用deal()改造后,简洁相当多 75 | [A,B,C,D] = deal(zeros(3,4)); 76 | ``` 77 | 78 | ##### 2.符号函数、内联函数、匿名函数 79 | 80 | 符号函数举例: 81 | 82 | ``` 83 | syms x 84 | y=x+1; 85 | y1=diff(y); %求导 86 | ``` 87 | 88 | 匿名函数举例: 89 | 90 | ``` 91 | z=@(x)(x(1)+2*x(2)); 92 | t=z([2 3]); %计算z在x=[2 3]处的值 93 | z=@(x,y)(x+2*y); 94 | t=z(2,3); %计算z在x=2且y=3处的值 95 | ``` 96 | 97 | 内联函数举例 98 | 99 | ``` 100 | >> h=inline('x+y','x','y') 101 | ``` 102 | 103 | 符号函数y转匿名函数f: 104 | 105 | ``` 106 | f = matlabFunction(y); 107 | ``` 108 | 109 | 匿名函数f转符号函数y: 110 | 111 | ``` 112 | syms x 113 | y = f(x); 114 | ``` 115 | 116 | ##### 3.执行字符串包含的matlab表达式:eval函数 117 | 118 | 对a1,a2,a3,a4,……,a100分别赋予1,2,3,……,100。 119 | 120 | ``` 121 |  for i=1:100 122 |      eval(['a' num2str(i) '=' num2str(i)]); 123 |  end 124 | ``` 125 | 126 | 批量存数据或图片文件 127 | 128 | -------------------------------------------------------------------------------- /docs/matlab技巧_快捷键_注意事项.md: -------------------------------------------------------------------------------- 1 | ### 📌Matlab 技巧、快捷键及注意事项 2 | 3 | #### 1. 一些常用及非常有效的 matlab 命令及使用技巧 4 | 5 | > 参考: 6 | > 7 | > 1. [What are some cool MATLAB tricks?](https://www.quora.com/What-are-some-cool-MATLAB-tricks) 8 | > 9 | > 2. [有哪些让人相见恨晚的 MATLAB 命令?](https://www.zhihu.com/question/24499729) 10 | 11 | * ##### 快捷键: 12 | 13 | * **ctrl + C**: 中断正在跑的程序 14 | * **ctrl + R**: 多行注释 15 | * **ctrl + T**: 多行去除注释 16 | * **ctrl + i**: 自动缩进对齐 17 | * **shift + TAB**: 多行向右缩进 18 | * **TAB**: 自动补全 19 | * **ALT:**如果要复制或删除多列数据(而不是若干行),或者要一次性编辑多行,该功能非常有用。要选择一个矩形区域,请在进行选择时按**Alt**键。 20 | * ##### 清理 21 | 22 | ```matlab 23 | >> clc % 清空Command Window 24 | 25 | >> close all % 关闭所有图片窗口 26 | 27 | >> close(h) % 关闭图片窗口h (e.g., h = figure(1)) 28 | 29 | >> clf % 清理当前图片窗口 30 | 31 | >> clear % 清空所有变量 32 | 33 | >> clear var1 var2 % 清理变量var1 var2 (注意用空格分隔) 34 | 35 | >> clearvars -except var3 var4 % 理除var3 var4以外的全部变量 36 | 37 | >> save filename.mat var1 var2 %保存变量var1/var2至文件filename.mat中,支持通配符* 38 | ``` 39 | * ##### 查询: 40 | 41 | > \[注意\] help, doc, edit 命令也可用于自定义函数,其帮助程度取决于该自定义函数的注释规范程度 42 | 43 | * ```matlab 44 | >> help matlab_fun % Matlab内置函数matlab_fun的帮助(简略) 45 | 46 | >> doc matlab_fun % Matlab内置函数matlab_fun的说明文档(详细) 47 | 48 | >> edit matlab_fun % 打开内置函数matlab_fun的script (source code) 49 | 50 | >> demo matlab_fun % 打开demo程序 51 | 52 | >> diary on; 53 | 54 | >> diary off; 55 | 56 | >> diary filename; %保存命令历史记录 57 | 58 | >> pwd % 当前文本路径 59 | 60 | >> which some_fun % 找出build-in函数或自定义函数的具体路径 61 | 62 | >> class(var) % 获取变量var的类型 63 | 64 | >> dir() % 当前路径下的所有文件 65 | 66 | >> dir(’some_path\’) % 某路径下的所有文件 67 | 68 | >> dir(’some_path\*.m’) % 某路径下所有的matlab文件(同理,.jpg, .csv文件等等) 69 | ``` 70 | * ##### 画图 71 | 72 | ```matlab 73 | >> title({‘line 01’, ‘line 02’, ’line 03’}) % 画图中的标题分成多行 74 | 75 | % 标题,坐标轴等均可使用LaTeX格式 76 | >> title('$$ax = b$$','interpreter','latex') 77 | >> xlabel(('$$ax = b$$','interpreter','latex') 78 | 79 | % 设定图像的坐标轴取值范围(统一取值范围,方便比较多幅图) 80 | >> axis([xmin, xmax, ymin, ymax]) 81 | % 设定图像的颜色取值范围 (统一颜色深浅范围,方便比较heatmap) 82 | >> caxis([cmin, cmax]) 83 | 84 | >> axis on (off) %(不)显示坐标轴 85 | >> grid on (off) % (不)添加网格 86 | 87 | % 自定义legend名称 88 | >> figure 89 | >> plot(x1, y1, ’DisplayName’, ’legend01’); hold on 90 | >> plot(x2,y2, ’DisplayName’, ’legend02’); hold off 91 | >> legend(’show') 92 | 93 | % 同时画很多图时,可以让它们dock在同一个窗口,避免出现无数悬窗 94 | >> h1 = figure(); h2 = figure(); 95 | >> set(h1, 'WindowStyle','docked') 96 | >> set(h2, 'WindowStyle','docked') 97 | ``` 98 | * ##### 其它 99 | * ```matlab 100 | >> addpath(genpath(pwd)) % 添加当前目录的所有子目录到搜索路径中 101 | 102 | % 程序运行完毕后提示音 103 | >> beep on; 104 | >> for i = 1:1e3; end % script: 运行完毕后有提示音 105 | >> beep 106 | 107 | % 程序运行时间 108 | >> tic 109 | >> for i = 1:1e3; end % script 运行时间 110 | >> t = toc %disp('程序的运行时间为'+num2str(toc)) 111 | 112 | eval(['save myfile',int2str(n),'.mat']) % Not recommended 113 | 114 | % 程序运行出错时停止 115 | >> dbstop if error 116 | >> MyScript % MyScript中的函数出错时,停止在错误处,而不是返回错误并且中断 117 | 118 | %c语言中的三目运算符a=(b>0)?1:2; 119 | %但可以用这种简洁的形式完成: 120 | >> a=1*(b>0)+2*(b<=0); 121 | %甚至更多的判断结果也可以用: 122 | >> a=1*(b>0)+2*(b==0)+3*(b<0); 123 | ``` 124 | 125 | #### 2.一些注意事项 126 | 127 | * 编程隐患 变量名、函数名字母大小写敏感,以字母开头,最大字符数namelengthmax。 128 | * 那种既可以用向量可以解决的问题,也可以用循环解决的问题,最好用向量解决,这 129 | 是因为向量执行的速度快。 130 | * 如果用可能的话,可用逻辑函数选择数组中的元素。如果逻辑数组进行运算,要比循 131 | 环快得多。 132 | 133 | 134 | 135 | --------------------------------------------------------------------------------