-
Notifications
You must be signed in to change notification settings - Fork 2
/
matlab_elements.html
400 lines (372 loc) · 37.4 KB
/
matlab_elements.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
<!DOCTYPE HTML>
<!--
Phantom by HTML5 UP
html5up.net | @ajlkn
Free for personal and commercial use under the CCA 3.0 license (html5up.net/license)
-->
<html>
<head>
<title>MATLAB Elements</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
<link rel="stylesheet" href="assets/css/main.css" />
<noscript><link rel="stylesheet" href="assets/css/noscript.css" /></noscript>
</head>
<body class="is-preload">
<!-- Wrapper -->
<div id="wrapper">
<!-- Header -->
<header id="header">
<div class="inner">
<!-- Logo -->
<a href="index.html" class="logo">
<span class="symbol"><img src="images/NeuroNestLogo.png" alt="NeuroNest Logo" /></span><span class="title">NeuroNest</span>
</a>
<!-- Nav -->
<nav>
<ul>
<li><a href="#menu">Menu</a></li>
</ul>
</nav>
</div>
</header>
<!-- Menu -->
<nav id="menu">
<h2>Menu</h2>
<ul>
<li><a href="index.html">Home</a></li>
<li><a href="resource_menu.html">Resources</a></li>
<li><a href="https://sopkoc.wixsite.com/neuronest/forum">Ask a Question</a></li>
<li><a href="https://sopkoc.wixsite.com/neuronest/about">About NeuroNest</a></li>
<li><a href="https://sopkoc.wixsite.com/neuronest/contact">Contact</a></li>
</ul>
</nav>
<!-- Main -->
<div id="main">
<div class="inner">
<h1 id="matlab-elements-and-syntax">MATLAB Elements and Syntax</h1>
<h2 id="1-basic-elements">1. Basic Elements</h2>
<p><strong>Comment:</strong> A comment in MATLAB is a piece of text in a script or function that is not executed. Comments are used to explain code and are created using the percent sign. For example, <code>x = 10 % This is a comment</code> assigns 10 to x and includes a comment. You can also write a block of comments using the block comment operators % { and % }.</p>
<p><strong>Variable:</strong> A variable in MATLAB is a storage location identified by a name that can hold different types of data such as numbers, arrays, or strings. Variables are created by assignment statements, for example, <code>x = 5</code> creates a variable x with a value of 5. </p>
<p><strong>String:</strong> A string is a series of characters, sometimes called a character vector or character array. Strings are created using single quotes, e.g., <code>str = 'hello'</code> creates a string variable <code>str</code> with the text <code>hello</code>.</p>
<p><strong>Array:</strong> An array is a data structure in that can store values of the same type. Arrays are created using square brackets and can be one-dimensional (e.g., vectors) or multi-dimensional (e.g., matrices). </p>
<ul>
<li><strong>Vector:</strong> A vector is a one-dimensional array that can hold a sequence of variables Vectors can be either row vectors or column vectors.<ul>
<li>A row vector is created by enclosing the elements in square brackets, separated by spaces or commas, e.g., 'rowVec = [1, 2, 3]' creates a 1x3 row vector.</li>
<li>A column vector is created by enclosing the elements in square brackets, separated by semicolons, e.g., 'colVec = [1; 2; 3]' creates a 3x1 column vector.</li>
</ul>
</li>
<li><strong>Matrix:</strong> A matrix is a two-dimensional array of numbers arranged in rows and columns. Matrices are created using square brackets with rows separated by semicolons, e.g., <code>A = [1, 2, 3; 4, 5, 6]</code> creates a 2x3 matrix. </li>
<li><strong>String Array:</strong>: A string array is a one-dimensional or multidimensional array that holds strings. For example, <code>strArray = ["Hello", "World"]</code> creates a 1x2 string array.</li>
</ul>
<p><strong>Cell:</strong> A cell (sometimes referred to as a cell array) is a data structure in MATLAB that can hold different types of data in an array format. Each element of a cell array is called a cell, and it can contain different types or sizes of data. Cell arrays are created using curly braces, e.g., <code>C = {1, 'text', [1, 2, 3]}</code> creates a cell array with three elements: a number, a string, and a vector.</p>
<p><strong>Function:</strong> A function is a block of code designed to perform a specific task. Functions in MATLAB are defined using the <code>function</code> keyword and can accept inputs and return outputs. For example, <code>function y = add(x1, x2)</code> defines a function named <code>add</code> that takes two inputs and returns their sum. Functions help in organizing and reusing code. </p>
<p><strong>Structure:</strong> A structure is a data type in MATLAB that organizes data into fields, each containing a different type of data. Structures are useful for grouping related data. They are created using the <code>struct</code> function or by directly assigning values to fields, e.g., <code>S.name = 'John'; S.age = 25;</code> creates a structure <code>S</code> with fields <code>name</code> and <code>age</code>.</p>
<p><strong>Table:</strong> A table is a data type in MATLAB for storing column-oriented or tabular data, such as in a spreadsheet or database. Tables are created using the <code>table</code> function, and they can store variables of different types and sizes, e.g., <code>T = table(Age, Height, Weight)</code> where <code>Age</code>, <code>Height</code>, and <code>Weight</code> are variables representing columns.</p>
<p>To help you identify elements, some entries appear in different colors in the Command Window and Editor. By default:</p>
<ul>
<li>Keywords are blue.</li>
<li>Character vectors and strings are purple.</li>
<li>Unterminated character vectors are maroon.</li>
<li>Comments are green.</li>
</ul>
<p><img width="295" alt="image" src="https://github.com/user-attachments/assets/be066590-ca88-4ac0-8d40-7ee8c90022ee"></p>
<h2 id="2-special-characters">2. Special Characters</h2>
<p>Below are some of the key special characters you should be familiar with, including delimiters which are what the lists of variables are enclosed by when creating arrays and cells. For detailed information about other special characters, see <a href="https://www.mathworks.com/help/matlab/matlab_prog/matlab-operators-and-special-characters.html">MATLAB Operators and Special Characters</a>. </p>
<p><strong>Square Brackets <code>[]</code>:</strong> </p>
<ul>
<li>A delimiter used to create arrays and matrices<ul>
<li>Example: <code>A = [1, 2, 3])</code>.</li>
</ul>
</li>
</ul>
<p><strong>Curly Braces <code>{}</code>:</strong></p>
<ul>
<li>A delimiter used to create cells<ul>
<li>Example: <code>C = {1, 'text', [1, 2, 3]})</code>.</li>
</ul>
</li>
<li>Indexing cells<ul>
<li>Example: <code>cellArray{index} = newValue</code>.</li>
</ul>
</li>
</ul>
<p><strong>Parentheses <code>()</code>:</strong></p>
<ul>
<li>Group expressions<ul>
<li>Examples: <code>a = (b + c) * d)</code>.</li>
</ul>
</li>
<li>Function arguments<ul>
<li>Example: <code>result = myFunction(arg1, arg2)</code>.</li>
</ul>
</li>
<li>Indexing arrays<ul>
<li>Example: <code>element = A(row, column)</code>.</li>
</ul>
</li>
</ul>
<p><strong>Comma <code>,</code></strong></p>
<ul>
<li>Separates elements in arrays and function arguments.<ul>
<li>Example: <code>[1, 2, 3]</code> or <code>plot(x, y)</code>.</li>
</ul>
</li>
</ul>
<p><strong>Colon <code>:</code></strong></p>
<ul>
<li>Creates vectors and specify ranges, and index arrays.<ul>
<li>Example: <code>1:5</code> generates a vector <code>[1, 2, 3, 4, 5]</code>.</li>
</ul>
</li>
<li>The colon alone, without start or end values, specifies all of the elements in that dimension. <ul>
<li>Example: <code>A(:, 2)</code> selects all rows from the second column of matrix <code>A</code>.</li>
</ul>
</li>
</ul>
<p><strong>Semicolon <code>;</code></strong></p>
<ul>
<li>Suppresses the output of a command.<ul>
<li>Example: <code>a = 5;</code> assigns 5 to <code>a</code> without displaying the output.</li>
</ul>
</li>
<li>Separates rows in matrix definitions.<ul>
<li>Example: <code>A = [1, 2; 3, 4]</code>.</li>
</ul>
</li>
</ul>
<p><strong>Ellipsis <code>...</code></strong></p>
<ul>
<li>Indicates that a statement continues on the next line.<ul>
<li>Example:<pre><code class="lang-matlab">a = <span class="hljs-number">1</span> + <span class="hljs-number">2</span> + <span class="hljs-number">3</span> + ...
<span class="hljs-number">4</span> + <span class="hljs-number">5</span> + <span class="hljs-number">6</span>;
</code></pre>
<strong>Double Percent Signs <code>%%</code></strong></li>
</ul>
</li>
<li>Starts a code section and is typically followed by the section title<ul>
<li>Example: <code>%% first code section</code> starts a section titled <code>first code section</code> without displaying the output.</li>
</ul>
</li>
</ul>
<h2 id="3-accessing-and-manipulating-elements">3. Accessing and Manipulating Elements</h2>
<p><strong>Indicies</strong>: Indices are used to specify the position of the element(s) so that you can retrieve them without processing the entire dataset. This capability enables you to easily update or modify specific elements within data structures or extract relevant portions of data for analysis or visualization.</p>
<p>For one-dimensional arrays or cells, you specify the element's position using a single index, which corresponds to the element's position in that dimension. For example:</p>
<pre><code class="lang-matlab">rowVector = [<span class="hljs-number">10</span>, <span class="hljs-number">20</span>, <span class="hljs-number">30</span>, <span class="hljs-number">40</span>, <span class="hljs-number">50</span>];
thirdElement = rowVector(<span class="hljs-number">3</span>); % The <span class="hljs-number">3</span> is an index for third element in the row, which is <span class="hljs-number">30</span>
colVector = [<span class="hljs-number">10</span>; <span class="hljs-number">20</span>; <span class="hljs-number">30</span>; <span class="hljs-number">40</span>; <span class="hljs-number">50</span>];
fourthElement =colVector(<span class="hljs-number">4</span>); % The <span class="hljs-number">4</span> is an index for fourth element in the column, which is <span class="hljs-number">40</span>
</code></pre>
<p>For multi-dimensional arrays or cells, you specify the element's position using a pair of indices (or more for higher dimensions), which correspond to the element's row and column positions. For example:</p>
<pre><code class="lang-matlab">matrix = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>; <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>; <span class="hljs-number">7</span>, <span class="hljs-number">8</span>, <span class="hljs-number">9</span>];
element = matrix(<span class="hljs-number">2</span>, <span class="hljs-number">3</span>); % These indicies are specifying the element in the second row and third column, which is <span class="hljs-number">6</span>
</code></pre>
<p><strong>Accessing Elements</strong>: Access element(s) with the appropriate indicies and delimiter (e.g., parantheses or cell braces).To access a subset of elements, you must specify the range using the colon <code>:</code> operator. For example:</p>
<pre><code class="lang-matlab">
% Vectors
v = [<span class="hljs-number">10</span>, <span class="hljs-number">20</span>, <span class="hljs-number">30</span>, <span class="hljs-number">40</span>, <span class="hljs-number">50</span>];
v(<span class="hljs-number">2</span>); % Accesses <span class="hljs-keyword">the</span> <span class="hljs-keyword">second</span> element, <span class="hljs-literal">result</span> <span class="hljs-keyword">is</span> <span class="hljs-number">20</span>
v(<span class="hljs-number">2</span>:<span class="hljs-number">4</span>); % Accesses elements <span class="hljs-keyword">from</span> <span class="hljs-keyword">the</span> <span class="hljs-keyword">second</span> <span class="hljs-keyword">to</span> <span class="hljs-keyword">the</span> <span class="hljs-keyword">fourth</span>, <span class="hljs-literal">result</span> <span class="hljs-keyword">is</span> [<span class="hljs-number">20</span>, <span class="hljs-number">30</span>, <span class="hljs-number">40</span>]
% Matrices
A = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>; <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>; <span class="hljs-number">7</span>, <span class="hljs-number">8</span>, <span class="hljs-number">9</span>];
A(<span class="hljs-number">2</span>,<span class="hljs-number">1</span>); % Accesses <span class="hljs-keyword">the</span> element <span class="hljs-keyword">in</span> <span class="hljs-keyword">the</span> <span class="hljs-keyword">second</span> row, <span class="hljs-keyword">first</span> column, <span class="hljs-literal">result</span> <span class="hljs-keyword">is</span> <span class="hljs-number">4</span>
A(<span class="hljs-number">1</span>:<span class="hljs-number">2</span>, <span class="hljs-number">2</span>:<span class="hljs-number">3</span>); % Accesses a submatrix <span class="hljs-keyword">from</span> rows <span class="hljs-number">1</span> <span class="hljs-keyword">to</span> <span class="hljs-number">2</span> <span class="hljs-keyword">and</span> columns <span class="hljs-number">2</span> <span class="hljs-keyword">to</span> <span class="hljs-number">3</span>, <span class="hljs-literal">result</span> <span class="hljs-keyword">is</span> [<span class="hljs-number">2</span>, <span class="hljs-number">3</span>; <span class="hljs-number">5</span>, <span class="hljs-number">6</span>]
% One-dimensional String Arrays
strArray = [<span class="hljs-string">"Hello"</span>, <span class="hljs-string">"World"</span>, <span class="hljs-string">"MATLAB"</span>];
strArray(<span class="hljs-number">1</span>); % Accesses <span class="hljs-keyword">the</span> <span class="hljs-keyword">first</span> element, <span class="hljs-literal">result</span> <span class="hljs-keyword">is</span> <span class="hljs-string">"Hello"</span>
strArray(<span class="hljs-number">1</span>:<span class="hljs-number">2</span>); % Accesses <span class="hljs-keyword">the</span> <span class="hljs-keyword">first</span> two elements, <span class="hljs-literal">result</span> <span class="hljs-keyword">is</span> [<span class="hljs-string">"Hello"</span>, <span class="hljs-string">"World"</span>]
% Multi-dimensional String Arrays
multiStrArray = [<span class="hljs-string">"Hello"</span>, <span class="hljs-string">"World"</span>; <span class="hljs-string">"MATLAB"</span>, <span class="hljs-string">"Programming"</span>];
multiStrArray(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>); % Accesses <span class="hljs-keyword">the</span> element <span class="hljs-keyword">in</span> <span class="hljs-keyword">the</span> <span class="hljs-keyword">first</span> row, <span class="hljs-keyword">second</span> column, <span class="hljs-literal">result</span> <span class="hljs-keyword">is</span> <span class="hljs-string">"World"</span>
multiStrArray(<span class="hljs-number">1</span>:<span class="hljs-number">2</span>, <span class="hljs-number">1</span>); % Accesses <span class="hljs-keyword">the</span> <span class="hljs-keyword">first</span> column, <span class="hljs-literal">result</span> <span class="hljs-keyword">is</span> [<span class="hljs-string">"Hello"</span>; <span class="hljs-string">"MATLAB"</span>]
% One-dimensional Cell Arrays
cellArray = {<span class="hljs-number">1</span>, 'MATLAB', [<span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>]};
cellArray{<span class="hljs-number">1</span>}; % Accesses <span class="hljs-keyword">the</span> <span class="hljs-keyword">first</span> element, <span class="hljs-literal">result</span> <span class="hljs-keyword">is</span> <span class="hljs-number">1</span>
cellArray{<span class="hljs-number">2</span>:<span class="hljs-number">3</span>}; % Accesses elements <span class="hljs-keyword">from</span> <span class="hljs-keyword">the</span> <span class="hljs-keyword">second</span> <span class="hljs-keyword">to</span> <span class="hljs-keyword">the</span> <span class="hljs-keyword">third</span>, <span class="hljs-literal">result</span> <span class="hljs-keyword">is</span> {'MATLAB', [<span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>]}
% Multi-dimensional Cell Arrays
multiCellArray = {<span class="hljs-number">1</span>, '<span class="hljs-built_in">text</span>'; <span class="hljs-number">2</span>, 'more <span class="hljs-built_in">text</span>'; <span class="hljs-number">3</span>, 'even more <span class="hljs-built_in">text</span>'};
cellArray{<span class="hljs-number">2</span>, <span class="hljs-number">1</span>}; % Accesses <span class="hljs-keyword">the</span> element <span class="hljs-keyword">in</span> <span class="hljs-keyword">the</span> <span class="hljs-keyword">second</span> row, <span class="hljs-keyword">first</span> column, <span class="hljs-literal">result</span> <span class="hljs-keyword">is</span> <span class="hljs-number">2</span>
multiCellArray{<span class="hljs-number">2</span>:<span class="hljs-number">3</span>, <span class="hljs-number">2</span>}; % Accesses elements <span class="hljs-keyword">from</span> rows <span class="hljs-number">2</span> <span class="hljs-keyword">to</span> <span class="hljs-number">3</span> <span class="hljs-keyword">in</span> column <span class="hljs-number">2</span>, <span class="hljs-literal">result</span> <span class="hljs-keyword">is</span> {'more <span class="hljs-built_in">text</span>', 'even more <span class="hljs-built_in">text</span>'}
</code></pre>
<p><strong>Modifying Elements</strong>: Change element(s) by using the <code>=</code> to assign a new value to the corresponding index. </p>
<pre><code class="lang-matlab">% Vectors
v = [<span class="hljs-number">10</span>, <span class="hljs-number">20</span>, <span class="hljs-number">30</span>, <span class="hljs-number">40</span>, <span class="hljs-number">50</span>];
v(<span class="hljs-number">2</span>) = <span class="hljs-number">25</span>; % Modifies <span class="hljs-keyword">the</span> <span class="hljs-keyword">second</span> element, <span class="hljs-literal">result</span> <span class="hljs-keyword">is</span> [<span class="hljs-number">10</span>, <span class="hljs-number">25</span>, <span class="hljs-number">30</span>, <span class="hljs-number">40</span>, <span class="hljs-number">50</span>]
v(<span class="hljs-number">2</span>:<span class="hljs-number">4</span>) = [<span class="hljs-number">21</span>, <span class="hljs-number">31</span>, <span class="hljs-number">41</span>]; % Modifies elements <span class="hljs-keyword">from</span> <span class="hljs-keyword">the</span> <span class="hljs-keyword">second</span> <span class="hljs-keyword">to</span> <span class="hljs-keyword">the</span> <span class="hljs-keyword">fourth</span>, <span class="hljs-literal">result</span> <span class="hljs-keyword">is</span> [<span class="hljs-number">10</span>, <span class="hljs-number">21</span>, <span class="hljs-number">31</span>, <span class="hljs-number">41</span>, <span class="hljs-number">50</span>]
% Matrices
A = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>; <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>; <span class="hljs-number">7</span>, <span class="hljs-number">8</span>, <span class="hljs-number">9</span>];
A(<span class="hljs-number">2</span>,<span class="hljs-number">1</span>) = <span class="hljs-number">40</span>; % Modifies <span class="hljs-keyword">the</span> element <span class="hljs-keyword">in</span> <span class="hljs-keyword">the</span> <span class="hljs-keyword">second</span> row, <span class="hljs-keyword">first</span> column, <span class="hljs-literal">result</span> <span class="hljs-keyword">is</span> [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>; <span class="hljs-number">40</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>; <span class="hljs-number">7</span>, <span class="hljs-number">8</span>, <span class="hljs-number">9</span>]
A(<span class="hljs-number">1</span>:<span class="hljs-number">2</span>, <span class="hljs-number">2</span>:<span class="hljs-number">3</span>) = [<span class="hljs-number">20</span>, <span class="hljs-number">30</span>; <span class="hljs-number">50</span>, <span class="hljs-number">60</span>]; % Modifies a submatrix <span class="hljs-keyword">from</span> rows <span class="hljs-number">1</span> <span class="hljs-keyword">to</span> <span class="hljs-number">2</span> <span class="hljs-keyword">and</span> columns <span class="hljs-number">2</span> <span class="hljs-keyword">to</span> <span class="hljs-number">3</span>, <span class="hljs-literal">result</span> <span class="hljs-keyword">is</span> [<span class="hljs-number">1</span>, <span class="hljs-number">20</span>, <span class="hljs-number">30</span>; <span class="hljs-number">40</span>, <span class="hljs-number">50</span>, <span class="hljs-number">60</span>; <span class="hljs-number">7</span>, <span class="hljs-number">8</span>, <span class="hljs-number">9</span>]
% One-dimensional String Arrays
strArray = [<span class="hljs-string">"Hello"</span>, <span class="hljs-string">"World"</span>, <span class="hljs-string">"MATLAB"</span>];
strArray(<span class="hljs-number">1</span>) = <span class="hljs-string">"Hi"</span>; % Modifies <span class="hljs-keyword">the</span> <span class="hljs-keyword">first</span> element, <span class="hljs-literal">result</span> <span class="hljs-keyword">is</span> [<span class="hljs-string">"Hi"</span>, <span class="hljs-string">"World"</span>, <span class="hljs-string">"MATLAB"</span>]
strArray(<span class="hljs-number">1</span>:<span class="hljs-number">2</span>) = [<span class="hljs-string">"Hey"</span>, <span class="hljs-string">"There"</span>]; % Modifies <span class="hljs-keyword">the</span> <span class="hljs-keyword">first</span> two elements, <span class="hljs-literal">result</span> <span class="hljs-keyword">is</span> [<span class="hljs-string">"Hey"</span>, <span class="hljs-string">"There"</span>, <span class="hljs-string">"MATLAB"</span>]
% Multi-dimensional String Arrays
multiStrArray = [<span class="hljs-string">"Hello"</span>, <span class="hljs-string">"World"</span>; <span class="hljs-string">"MATLAB"</span>, <span class="hljs-string">"Programming"</span>];
multiStrArray(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>) = <span class="hljs-string">"Everyone"</span>; % Modifies <span class="hljs-keyword">the</span> element <span class="hljs-keyword">in</span> <span class="hljs-keyword">the</span> <span class="hljs-keyword">first</span> row, <span class="hljs-keyword">second</span> column, <span class="hljs-literal">result</span> <span class="hljs-keyword">is</span> [<span class="hljs-string">"Hello"</span>, <span class="hljs-string">"Everyone"</span>; <span class="hljs-string">"MATLAB"</span>, <span class="hljs-string">"Programming"</span>]
multiStrArray(<span class="hljs-number">1</span>:<span class="hljs-number">2</span>, <span class="hljs-number">1</span>) = [<span class="hljs-string">"Hi"</span>; <span class="hljs-string">"Hello"</span>]; % Modifies <span class="hljs-keyword">the</span> <span class="hljs-keyword">first</span> column, <span class="hljs-literal">result</span> <span class="hljs-keyword">is</span> [<span class="hljs-string">"Hi"</span>, <span class="hljs-string">"Everyone"</span>; <span class="hljs-string">"Hello"</span>, <span class="hljs-string">"Programming"</span>]
% One-dimensional Cell Arrays
cellArray = {<span class="hljs-number">1</span>, 'MATLAB', [<span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>]};
cellArray{<span class="hljs-number">1</span>} = <span class="hljs-number">10</span>; % Modifies <span class="hljs-keyword">the</span> <span class="hljs-keyword">first</span> element, <span class="hljs-literal">result</span> <span class="hljs-keyword">is</span> {<span class="hljs-number">10</span>, 'MATLAB', [<span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>]}
cellArray{<span class="hljs-number">2</span>:<span class="hljs-number">3</span>} = {'Hi', [<span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">7</span>]}; % Modifies elements <span class="hljs-keyword">from</span> <span class="hljs-keyword">the</span> <span class="hljs-keyword">second</span> <span class="hljs-keyword">to</span> <span class="hljs-keyword">the</span> <span class="hljs-keyword">third</span>, <span class="hljs-literal">result</span> <span class="hljs-keyword">is</span> {<span class="hljs-number">10</span>, 'Hi', [<span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">7</span>]}
% Multi-dimensional Cell Arrays
multiCellArray = {<span class="hljs-number">1</span>, '<span class="hljs-built_in">text</span>'; <span class="hljs-number">2</span>, 'more <span class="hljs-built_in">text</span>'; <span class="hljs-number">3</span>, 'even more <span class="hljs-built_in">text</span>'};
multiCellArray{<span class="hljs-number">2</span>, <span class="hljs-number">1</span>} = <span class="hljs-number">20</span>; % Modifies <span class="hljs-keyword">the</span> element <span class="hljs-keyword">in</span> <span class="hljs-keyword">the</span> <span class="hljs-keyword">second</span> row, <span class="hljs-keyword">first</span> column, <span class="hljs-literal">result</span> <span class="hljs-keyword">is</span> {<span class="hljs-number">1</span>, '<span class="hljs-built_in">text</span>'; <span class="hljs-number">20</span>, 'more <span class="hljs-built_in">text</span>'; <span class="hljs-number">3</span>, 'even more <span class="hljs-built_in">text</span>'}
multiCellArray{<span class="hljs-number">2</span>:<span class="hljs-number">3</span>, <span class="hljs-number">2</span>} = {'new <span class="hljs-built_in">text</span>', 'latest <span class="hljs-built_in">text</span>'}; % Modifies elements <span class="hljs-keyword">from</span> rows <span class="hljs-number">2</span> <span class="hljs-keyword">to</span> <span class="hljs-number">3</span> <span class="hljs-keyword">in</span> column <span class="hljs-number">2</span>, <span class="hljs-literal">result</span> <span class="hljs-keyword">is</span> {<span class="hljs-number">1</span>, '<span class="hljs-built_in">text</span>'; <span class="hljs-number">20</span>, 'new <span class="hljs-built_in">text</span>'; <span class="hljs-number">3</span>, 'latest <span class="hljs-built_in">text</span>'}
</code></pre>
<h2 id="4-case-and-space-sensitivity">4. Case and Space Sensitivity</h2>
<p>MATLAB is <strong>case-sensitive</strong>, meaning that variable and function names like <code>VariableName</code> and <code>variablename</code> are considered different.</p>
<p>Additionally, MATLAB is <em>somewhat</em> <strong>space-sensitive</strong>, meaning spaces around operators, assignments, and in-function calls are mostly optional and do not affect code execution. While spaces are generally not significant in MATLAB syntax, improper placement can sometimes cause errors or affect readability. For Example:</p>
<pre><code class="lang-matlab"> % These are all equivalent
y = sin(x)<span class="hljs-comment">;</span>
y=sin(x)<span class="hljs-comment">;</span>
y = sin( x )<span class="hljs-comment">;</span>
% <span class="hljs-keyword">Both </span>create the same vector
A = [<span class="hljs-number">1</span> <span class="hljs-number">2</span> <span class="hljs-number">3</span>]<span class="hljs-comment">;</span>
A = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>]<span class="hljs-comment">;</span>
% These are also equivalent
result = max( a, <span class="hljs-keyword">b </span>)<span class="hljs-comment">;</span>
result=max(a,<span class="hljs-keyword">b);
</span>
% Each would work with <span class="hljs-keyword">or </span>without spaces
a=<span class="hljs-number">1</span><span class="hljs-comment">; % No spaces</span>
<span class="hljs-keyword">b </span>= <span class="hljs-number">2</span><span class="hljs-comment">; % With spaces</span>
c = <span class="hljs-number">3</span> + <span class="hljs-number">4</span><span class="hljs-comment">; % With spaces around the operator</span>
d=<span class="hljs-number">3</span>+<span class="hljs-number">4</span><span class="hljs-comment">; % No spaces around the operator</span>
</code></pre>
<p>However, spaces within strings and in the definition of matrices and arrays must be used correctly. For example:</p>
<pre><code class="lang-matlab"> str1 = 'Hello';
str2 = 'Hello ';
isEqual = strcmp(str1, str2); % This will be false because of the trailing space in str2
rowVec = [<span class="hljs-number">1</span> <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span> <span class="hljs-number">5</span>]; % MATLAB might not interpret this correctly due to mixed delimiters
rowVec = [<span class="hljs-number">1</span> <span class="hljs-number">2</span> <span class="hljs-number">3</span>,<span class="hljs-number">4</span>, <span class="hljs-number">5</span>]; % Might still run but is hard to read and maintain
A = [<span class="hljs-number">1</span> <span class="hljs-number">2</span> <span class="hljs-number">3</span> <span class="hljs-number">4</span> <span class="hljs-number">5</span>]; % Consistent use of spaces
B = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>]; % Consistent use of commas
</code></pre>
<p>For more information, see <a href="https://www.mathworks.com/help/matlab/matlab_prog/case-and-space-sensitivity.html">Case and Space Sensitivity</a></p>
<p><strong>Code Style</strong>: </p>
<p>Note that while MATLAB allows for flexible spacing, consistent use of spaces enhances code readability and maintainability. This is especially important in collaborative environments or when sharing code with others. </p>
<p>Adopting a consistent coding style, including the use of spaces, helps in maintaining clean and understandable code. MATLAB's <a href="https://www.mathworks.com/help/matlab/matlab_prog/use-the-matlab-code-analyzer.html">code analyzer</a> provides guidelines and warnings to improve code quality, including spacing issues. </p>
<h2 id="5-code-suggestions-and-completions">5. Code Suggestions and Completions</h2>
<p>MATLAB offers real-time syntax checking and code suggestions. As you type in the Command Window or Editor, MATLAB will suggest function names, variable names, and other elements. MATLAB also indicates matched and mismatched delimiters (e.g., parentheses and brackets) and paired language keywords (e.g., for, if, while, else, and end statements). For more information, see <a href="https://www.mathworks.com/help/matlab/matlab_env/check-syntax-as-you-type.html">Check Syntax as You Type</a>.</p>
<h2 id="6-scripts-vs-functions">6. Scripts vs Functions</h2>
<p><strong>Scripts</strong>: Scripts are collections of MATLAB commands executed exactly as if they were typed into the Command Window. They operate within the current workspace, which means they can modify any existing variables or create new ones. Scripts do not accept inputs directly (though they can work with data already in the workspace) and do not return outputs. Scripts are useful for automating a series of MATLAB commands and for writing code that does not need to be modular or reusable, like data manipulation and plotting. </p>
<p><strong>Functions</strong>: Functions, on the other hand, are more versatile and encapsulate their code within a separate workspace. They can accept inputs and return outputs, making them essential for modular programming. Functions prevent potential conflicts with the main workspace by keeping variables local, except those explicitly returned. They are ideal for reusing code, enhancing code readability, and managing larger projects where data encapsulation is necessary.</p>
<h2 id="7-command-cheat-sheet">7. Command Cheat Sheet</h2>
<table>
<thead>
<tr>
<th>Command</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong>Viewing Elements</strong></td>
<td></td>
</tr>
<tr>
<td><code>who</code></td>
<td>Lists the names of all elements currently in the workspace. Does not display values.</td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td><code>whos</code></td>
<td>Lists all elements in the workspace along with detailed information about each variable, such as size, bytes, class (data type), and attributes. Does not display values.</td>
</tr>
<tr>
<td><code>disp(element)</code></td>
<td>Displays the value of the specified element.</td>
</tr>
<tr>
<td><strong>Deleting Elements</strong></td>
</tr>
<tr>
<td><code>clear element</code></td>
<td>Deletes the specified element from the workspace.</td>
</tr>
<tr>
<td><code>clear all</code></td>
<td>Deletes all elements from the workspace.</td>
</tr>
<tr>
<td><code>clearvars varName1 varName2</code></td>
<td>Deletes specific element from the workspace.</td>
</tr>
<tr>
<td><code>clearvars -except varName1 var2</code></td>
<td>Deletes all elements from the workspace except the specified ones.</td>
</tr>
<tr>
<td><strong>Inspecting Elements</strong></td>
</tr>
<tr>
<td><code>size(element)</code></td>
<td>Returns the dimensions of an array.</td>
</tr>
<tr>
<td><code>length(element)</code></td>
<td>Returns the length of the largest array dimension.</td>
</tr>
<tr>
<td><code>class(element)</code></td>
<td>Returns the class of the element.</td>
</tr>
</tbody>
</table>
<h2 id="8-suggested-tutorials">8. Suggested Tutorials</h2>
<p><a href="https://matlabacademy.mathworks.com/details/matlab-fundamentals/mlbe#module=3">Tutorial: Creating and Manipulating Arrays</a></p>
<p><a href="https://matlabacademy.mathworks.com/details/matlab-fundamentals/mlbe#module=4">Tutorial: Accessing Data in Arrays</a></p>
<h2 id="9-supplemental-materials">9. Supplemental Materials</h2>
<p><a href="https://learnxinyminutes.com/docs/matlab/">Cheat Sheet: Example of Code Syntax and Formatting</a></p>
<p><a href="https://www.tutorialspoint.com/matlab/matlab_syntax.htm">Live Demo: Basic Syntax</a></p>
<p><a href="https://www.tutorialspoint.com/matlab/matlab_variables.htm">Live Demo: Variables Overview</a></p>
<p><a href="https://www.tutorialspoint.com/matlab/matlab_colon_notation.htm">Live Demo: Colon Notation</a></p>
<p><a href="https://www.tutorialspoint.com/matlab/matlab_matrics.htm">Live Demo: Matrices</a></p>
<p><a href="https://www.tutorialspoint.com/matlab/matlab_m_files.htm">Live Demo: Scripts</a></p>
<p><a href="https://www.mathworks.com/help/matlab/matrices-and-arrays.html?s_tid=CRUX_lftnav">Documentation: Matrices and Arrays</a></p>
<p><a href="https://www.mathworks.com/help/matlab/matlab_prog/create-and-run-sections.html">Documentation: Create and Run Sections in Code</a></p>
<p><a href="https://www.mathworks.com/help/matlab/matlab_prog/create-scripts.html">Documentation: Creating Scripts</a></p>
<p><a href="https://www.mathworks.com/help/matlab/scripts.html?s_tid=CRUX_lftnav">Documentation: Scripts</a></p>
<p><a href="https://www.youtube.com/watch?v=sGb_xNsVELw&list=PLn0OLiymPak2HkkG-NToKdP7ye7NlpC8D&index=5">Video: Variables for Numbers, Indexing</a></p>
<p><a href="https://andysbrainbook.readthedocs.io/en/latest/Matlab/Matlab_01_Navigation.html">Andy's Brain Book: Navigation and Matrices</a></p>
</div>
</div>
<!-- Place the bird image and text anywhere on the page -->
<div style="text-align: right; padding-right: 40px;">
<a href="matlab_data_types.html">
<img src="images/small_bird_arrow.png" alt="Next Page" style="width: 100px; height: 100px;">
<div style="font-size: 18px; margin-top: 10px;">Next Page</div>
</a>
</div>
<!-- Footer -->
<footer id="footer">
<div class="inner">
<section>
<h2>Funding</h2>
<p> We would like to express our heartfelt gratitude to <strong>Neurohackademy</strong> at the <strong>University of Washington eScience Institute</strong> for providing invaluable training and support. This experience has significantly enriched our understanding of neuroimaging and data science. We also acknowledge the support of the National Institute of Mental Health (NIMH) grant number <strong>5R25MH112480-08</strong>, which made this opportunity possible.</p>
</section>
<section>
<h2>Follow</h2>
<ul class="icons">
<li><a href="https://x.com/Neuro_Nest" class="icon brands style2 fa-twitter"><span class="label">Twitter</span></a></li>
<li><a href="https://github.com/NeuroHackademy2024/NeuroNest" class="icon brands style2 fa-github"><span class="label">GitHub</span></a></li>
<li><a href="mailto:[email protected]" class="icon solid style2 fa-envelope"><span class="label">Email</span></a></li>
</ul>
</section>
<ul class="copyright">
<li>© Untitled. All rights reserved</li><li>Design: <a href="http://html5up.net">HTML5 UP</a></li>
</ul>
</div>
</footer>
</div>
<!-- Scripts -->
<script src="assets/js/jquery.min.js"></script>
<script src="assets/js/browser.min.js"></script>
<script src="assets/js/breakpoints.min.js"></script>
<script src="assets/js/util.js"></script>
<script src="assets/js/main.js"></script>
</body>
</html>