forked from swcarpentry/matlab-novice-inflammation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02-scripts.html
108 lines (98 loc) · 6.86 KB
/
02-scripts.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="generator" content="pandoc">
<title>Software Carpentry: Programming with MATLAB</title>
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" type="text/css" href="css/bootstrap/bootstrap.css" />
<link rel="stylesheet" type="text/css" href="css/bootstrap/bootstrap-theme.css" />
<link rel="stylesheet" type="text/css" href="css/swc.css" />
<link rel="alternate" type="application/rss+xml" title="Software Carpentry Blog" href="http://software-carpentry.org/feed.xml"/>
<meta charset="UTF-8" />
<!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body class="lesson">
<div class="container card">
<div class="banner">
<a href="http://software-carpentry.org" title="Software Carpentry">
<img alt="Software Carpentry banner" src="img/software-carpentry-banner.png" />
</a>
</div>
<article>
<div class="row">
<div class="col-md-10 col-md-offset-1">
<h1 class="title">Programming with MATLAB</h1>
<h2 class="subtitle">Writing MATLAB Scripts</h2>
<div id="learning-objectives" class="objectives panel panel-warning">
<div class="panel-heading">
<h2><span class="glyphicon glyphicon-certificate"></span>Learning Objectives</h2>
</div>
<div class="panel-body">
<ul>
<li>Learn how to write and save MATLAB scripts.</li>
<li>Learn how to save MATLAB plots to disk.</li>
</ul>
</div>
</div>
<p>So far, we’ve typed in commands one-by-one on the command line to get MATLAB to do things for us. But what if we want to repeat our analysis? Sure, it’s only a handful of commands, and typing them in shouldn’t take us more than a few minutes. But if we forget a step or make a mistake, we’ll waste time rewriting commands. Also, we’ll quickly find ourselves doing more complex analyses, and we’ll need our results to be more easily reproducible.</p>
<p>In addition to running MATLAB commands one-by-one on the command line, we can also write several commands in a <em>script</em>. A MATLAB script is just a text file with a <code>.m</code> extension. We’ve written commands to load data from a <code>.csv</code> file and displays some statistics about that data. Let’s put those commands in a script called <code>analyze.m</code>:</p>
<pre class="sourceCode matlab"><code class="sourceCode matlab"><span class="co">% script analyze.m</span>
patient_data = csvread(<span class="st">'inflammation-01.csv'</span>);
disp([<span class="st">'Analyzing "inflammation-01.csv": '</span>])
disp([<span class="st">'Maximum inflammation: '</span>, num2str(max(patient_data(:)))]);
disp([<span class="st">'Minimum inflammation: '</span>, num2str(min(patient_data(:)))]);
disp([<span class="st">'Standard deviation: '</span>, num2str(std(patient_data(:)))]);</code></pre>
<p>Before we can use it, we need to make sure that this file is visible to MATLAB. MATLAB doesn’t know about all the files on your computer, but it keeps an eye on several directories. The most convenient of these directories is generally the “working directory”, or “current directory”. To find out the working directory, use the <code>pwd</code> command:</p>
<pre class="sourceCode matlab"><code class="sourceCode matlab">pwd</code></pre>
<p>As you might have guessed, <code>pwd</code> stands for “print working directory”.</p>
<p>Once you have a script saved in a location that MATLAB knows about, you can get MATLAB to run those commands by typing in the name of the script (without the <code>.m</code>) in the MATLAB command line:</p>
<pre class="sourceCode matlab"><code class="sourceCode matlab">analyze</code></pre>
<pre class="sourceCode matlab"><code class="sourceCode matlab">Maximum inflammation: <span class="fl">20</span>
Minimum inflammation: <span class="fl">0</span>
Standard deviation: <span class="fl">4.7219</span></code></pre>
<p>We’ve also written commands to create plots:</p>
<pre class="sourceCode matlab"><code class="sourceCode matlab">ave_inflammation = mean(patient_data, <span class="fl">1</span>);
plot(ave_inflammation);
ylabel(<span class="st">'average'</span>)</code></pre>
<p>MATLAB let’s us save those as images on disk:</p>
<pre class="sourceCode matlab"><code class="sourceCode matlab"><span class="co">% save plot to disk as png image:</span>
print -dpng <span class="st">'average.png'</span></code></pre>
<p>You might have noticed that we described what we want our code to do using the <code>%</code>-sign. This is another plus of writing scripts: you can comment your code to make it easier to understand when you come back to it after a while.</p>
<p>Let’s extend our <code>analyze</code> script with commands to create and save plots:</p>
<pre class="sourceCode matlab"><code class="sourceCode matlab"><span class="co">% script analyze.m</span>
patient_data = csvread(<span class="st">'inflammation-01.csv'</span>);
disp([<span class="st">'Maximum inflammation: '</span>, num2str(max(patient_data(:)))]);
disp([<span class="st">'Minimum inflammation: '</span>, num2str(min(patient_data(:)))]);
disp([<span class="st">'Standard deviation: '</span>, num2str(std(patient_data(:)))]);
ave_inflammation = mean(patient_data, <span class="fl">1</span>);
subplot(<span class="fl">1</span>, <span class="fl">3</span>, <span class="fl">1</span>);
plot(ave_inflammation);
ylabel(<span class="st">'average'</span>)
subplot(<span class="fl">1</span>, <span class="fl">3</span>, <span class="fl">2</span>);
plot(max(patient_data, [], <span class="fl">1</span>));
ylabel(<span class="st">'max'</span>)
subplot(<span class="fl">1</span>, <span class="fl">3</span>, <span class="fl">3</span>);
plot(min(patient_data, [], <span class="fl">1</span>));
ylabel(<span class="st">'min'</span>)
<span class="co">% save plot to disk as png image:</span>
print -dpng <span class="st">'patient_data-01.png'</span></code></pre>
</div>
</div>
</article>
<div class="footer">
<a class="label swc-blue-bg" href="http://software-carpentry.org">Software Carpentry</a>
<a class="label swc-blue-bg" href="https://github.com/swcarpentry/matlab-novice-inflammation">Source</a>
<a class="label swc-blue-bg" href="mailto:[email protected]">Contact</a>
<a class="label swc-blue-bg" href="LICENSE.html">License</a>
</div>
</div>
<!-- Javascript placed at the end of the document so the pages load faster -->
<script src="http://software-carpentry.org/v5/js/jquery-1.9.1.min.js"></script>
<script src="css/bootstrap/bootstrap-js/bootstrap.js"></script>
</body>
</html>