{"id":225,"date":"2021-06-07T15:29:38","date_gmt":"2021-06-07T07:29:38","guid":{"rendered":"https:\/\/swordofmorning.com\/?p=225"},"modified":"2025-10-09T13:56:21","modified_gmt":"2025-10-09T05:56:21","slug":"matlab-tutorial-02-structured-programs-and-data-access","status":"publish","type":"post","link":"https:\/\/swordofmorning.com\/index.php\/2021\/06\/07\/matlab-tutorial-02-structured-programs-and-data-access\/","title":{"rendered":"Matlab Tutorial 02 Structured Programs and Data Access"},"content":{"rendered":"<p><div class=\"has-toc have-toc\"><\/div><\/p>\n<h2>\u4e00\u3001\u6ce8\u91ca\u7b26\u53f7\u7684\u4f7f\u7528<\/h2>\n<p>&nbsp;&nbsp;&nbsp;&nbsp;\u5355\u4e2a%\u7b26\u5408\u4e3a\u6ce8\u91ca\uff1b2\u4e2a%\u7b26\u53f7\u53ef\u4ee5\u4f5c\u4e3a\u4e00\u4e2a\u5c0f\u8282\uff08Section\uff09\u4f7f\u7528\uff0c\u5c0f\u8282\u5728Matlab\u4e2d\u53ef\u4ee5\u4f5c\u4e3a\u5355\u72ec\u4e00\u4e2a\u90e8\u5206\u8fd0\u884c\uff08\u800c\u4e0d\u5fc5\u8981\u8fd0\u884c\u6574\u4e2a\u7a0b\u5e8f\uff09\u3002<\/p>\n<h2>\u4e8c\u3001\u5c0f\u6280\u5de7<\/h2>\n<ul>\n<li>clear all\uff0c\u6e05\u9664\u6240\u6709\u53d8\u91cf<\/li>\n<li>close all\uff0c\u6e05\u9664\u6240\u6709\u56fe\u8868<\/li>\n<li>Ctrl + C\uff0c\u7ec8\u6b62\u7a0b\u5e8f<\/li>\n<\/ul>\n<h2>\u4e09\u3001\u8bfb\u5199\u6587\u4ef6<\/h2>\n<h3>3.1 Save<\/h3>\n<ul>\n<li>save <em>FileName<\/em>.mat\uff0c\u7528matlab\u7684\u538b\u7f29\u65b9\u5f0f\u5b58\u50a8\u3002<\/li>\n<li>save <em>FileName<\/em>.mat -ascii\uff0c\u5728\u6587\u672c\u7f16\u8f91\u5668\u4e2d\u53ef\u8bfb\u7684\u65b9\u5f0f\u5b58\u50a8\u3002<\/li>\n<\/ul>\n<h3>3.2 Load<\/h3>\n<ul>\n<li>load(\u2018<em>FileName<\/em>.mat\u2019);<\/li>\n<li>load(\u2018<em>FileName<\/em>.mat\u2019\uff0c \u2018-ascii\u2019);<\/li>\n<\/ul>\n<h3>3.3 \u8bfb\u53d6Excel\u6587\u4ef6\uff08\u53ea\u8bfb\u6570\u5b57\uff09<\/h3>\n<ul>\n<li>m = xlsread(\u2018<em>FileName<\/em>.xlsx\u2019);<\/li>\n<li>m = xlsread(\u2018<em>FileName<\/em>.xlsx\u2019, \u2018B2 : D4\u2019)\uff0c\u8bfb\u53d6\u7279\u5b9a\u533a\u95f4\u7684\u5185\u5bb9\u3002<\/li>\n<\/ul>\n<h3>3.4 \u5199\u5165Excel\u6587\u4ef6<\/h3>\n<ul>\n<li>xlswrite(\u2018<em>FileName<\/em>.xlsx\u2019, <em>VariableName<\/em>, ExcelSheet, \u2018E2 : E4\u2019);<\/li>\n<\/ul>\n<h3>3.3 \u8bfb\u53d6Excel\u6587\u4ef6\uff08\u6570\u5b57\u548c\u6587\u5b57\/\u8868\u5934\uff09<\/h3>\n<ul>\n<li>[num, Header] = xlsread(\u2018<em>FileName<\/em>.xlsx\u2019);<\/li>\n<\/ul>\n<h3>\u56db\u3001\u4e00\u4e9b\u6570\u636e\u7ed3\u6784<\/h3>\n<pre><code>%% \u5b57\u7b26\u4e32\u9006\u5e8f\nclear;\n\nA = &#039;String&#039;;\nB = A;\nsize_A = size(A);\nsize_A = size_A(2);\nsize_B = size_A;\n% size\u51fd\u6570\u8fd4\u56de\u7684\u662fn * n\u7684\u77e9\u9635\n\nfor i = 1 : size_B\n    B(i) = A(size_A - i + 1);\nend\n\ndisp(B);\n\n%% Structure\nclear;\nstudent.name = &#039;Jack&#039;;\nstudent.ID = 100;\nstudent.grade = 87;\n\nstudent(2).name = &#039;Mike&#039;;\nstudent(2).ID = 101;\nstudent(2).grade = 78;\n\ndisp(student(1));\ndisp(student(2));\n\n%% Cell \u53ef\u4ee5\u5f80\u4e00\u4e2a\u77e9\u9635\u4e2d\u52a0\u5165\u4e0d\u540c\u7684\u5143\u7d20\uff0c\u4ee5\u8fbe\u5230\u6307\u9488\u7684\u76ee\u7684\nclear;\nA(1, 1) = { [1, 2, 3] };\nA{1, 2} = { &#039;hello&#039; };\nA(2, 1) = { 12 };\nA(2, 2) = { [1, 2; 3, 4] };\n\ndisp(A);<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u6ce8\u91ca\u7b26\u53f7\u7684\u4f7f\u7528 &nbsp;&nbsp;&nbsp;&nbsp;\u5355\u4e2a%\u7b26\u5408\u4e3a\u6ce8\u91ca\uff1b2\u4e2a%\u7b26\u53f7\u53ef\u4ee5\u4f5c\u4e3a\u4e00\u4e2a\u5c0f\u8282\uff08Section\uff09 &#8230;<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[65],"tags":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/posts\/225"}],"collection":[{"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/comments?post=225"}],"version-history":[{"count":1,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/posts\/225\/revisions"}],"predecessor-version":[{"id":1038,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/posts\/225\/revisions\/1038"}],"wp:attachment":[{"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/media?parent=225"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/categories?post=225"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/tags?post=225"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}