Tutorial by Examples

Use the continuation character (ellipsis) ... to continue long statement. Example: MyFunc( parameter1,parameter2,parameter3,parameter4, parameter5, parameter6,parameter7, parameter8, parameter9) can be replaced by: MyFunc( parameter1, ... parameter2, ... parameter3, ... ...
Proper indentation gives not only the aesthetic look but also increases the readability of the code. For example, consider the following code: %no need to understand the code, just give it a look n = 2; bf = false; while n>1 for ii = 1:n for jj = 1:n if ii+jj>30 bf = true; break end...
Matlab allows some very trivial mistakes to go by silently, which might cause an error to be raised much later in the run - making debugging hard. If you assume something about your variables, validate it. function out1 = get_cell_value_at_index(scalar1,cell2) assert(isscalar(scalar1),'1st input ...
Most of the time, loops are computationally expensive with Matlab. Your code will be orders of magnitudes faster if you use vectorization. It also often makes your code more modular, easily modifiable, and easier to debug. The major downside is that you have to take time to plan the data structures,...
While coding a script or a function, it can be the case that one or more than one temporary file be needed in order to, for example, store some data. In order to avoid overwriting an existing file or to shadow a MATLAB function the tempname function can be used to generate a unique name for a te...
The function validateattributes can be used to validate an array against a set of specifications It can be therefore used to validate the input provided to a function. In the following example, the function test_validateattributes requires three input function test_validateattributes(input_1,in...
It is a good practice to add comments that describe the code. It is helpful for others and even for the coder when returned later. A single line can be commented using the % symbol or using the shortkey Ctrl+R. To uncomment a previously commented line remove the % symbol or use shortkey Crtl+T. W...

Page 1 of 1