JIYIK CN >

Current Location:Home > Learning > PROGRAM > MATLAB >

MATLAB Line Continuation

Author:JIYIK Last Updated:2025/04/18 Views:

.This tutorial will discuss how to continue a line using 3 dot ( ) symbols in Matlab .


MATLAB Line Continuation

Sometimes, while writing code in Matlab, we have to write very long lines of code, which is not good if we want to see the entire code. In Matlab, we can use 3 dots in a line after the comma ...and move to the next line to continue writing the code.

For example, let's define a vector using 3 points. See the code below.

My_string = [1,2,3,...
    4,5]

Output:

My_string =

     1     2     3     4     5

In the above code, we have passed 3 values ​​in the vector in the first row and then moved to the next row using 3 dots after the comma and passed another two values. We can see in the above output that all the values ​​are added to the same variable.

We can also do this in case of function parameters, define other data types, etc. For example, if we want to write a long string and we don’t want to write the entire string on one line, we can write the string on the first line, add a comma and 3 dots at the end of the line and move to the next line and write the next line of string on the second line, and so on.

For example, let us define a string using 3 dots. See the code below.

My_string = ['hello world',...
    ' hello']

Output:

My_string =

    'hello world hello'

Matlab automatically saves all the words as a single string in the above output because we defined the string using single quotes. If we define the string using double quotes, the output will not be a single string.

Therefore, we have to define the string as either single quotes or a character array in order for the above method to work. In the above code, if we want to write another line of string, we have to add a comma and three dots at the end of the second line before we can move to the third line.

We can also strcat()do the above operation using the concatenate function, which is used to concatenate strings. We have to save each line of the string in a unique variable and then we can strcat()pass all the variables in the concatenate function, which will concatenate them and return a string.

Using strcat()the function, we don't have to worry about single and double quotes because the function always returns a single string. For example, let's strcat()repeat the above example using the function.

See the code below.

s1 = 'hello'
s2 = ' world'
s3 = ' hello'
s4 = strcat(s1,s2,s3)

Output:

s1 =

    'hello'


s2 =

    ' world'


s3 =

    ' hello'


s4 =

    'hello world hello'

In the above code, we have to manually add spaces at the beginning of the string; otherwise, the words will be joined together without spaces. If we want to concatenate strings using spaces or any other delimiter, we can use Matlab's concatenate join()function.

We have to make sure that the strings are defined with double quotes and inside a vector. By default, join()the function adds spaces between each string, but if we want to add another delimiter, we can pass the delimiter as the second argument to join()the function.

For example, let us join()concatenate the above strings without adding a space at the beginning using the concatenation function. See the code below.

s = ["hello world",...
    "hello"]
s1 = join(s)
s2 = join(s,'-')

Output:

s =

  1×2 string array

    "hello world"    "hello"


s1 =

    "hello world hello"


s2 =

    "hello world-hello"

-We have concatenated the given string using space and _ delimiter in the above code . We can see in the output that the given string is not a single string as it is defined with double quotes.

If we use single quotes or curly braces to define the string, we cannot use join()the function. For strings defined using curly braces and single quotes, we can use strjoin()the function instead of join()the function to append the strings in the given array.

For example, let us repeat the above example using curly braces and single quotes. See the code below.

clc
clear

s = {'hello world',...
    'hello'}
s1 = strjoin(s)
s2 = strjoin(s,'-')

Output:

s =

  1×2 cell array

    {'hello world'}    {'hello'}


s1 =

    'hello world hello'


s2 =

    'hello world-hello'

We cannot define the above string using double quotes because Matlab will give an error stating that the first input must be a string array or a cell array of character vectors.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Commenting multiple lines in MATLAB

Publish Date:2025/04/18 Views:115 Category:MATLAB

This tutorial discusses how to comment multiple lines of code in MATLAB using the comment block method and the MATLAB Editor. Use comment blocks in MATLAB to comment multiple lines of code To comment one or two lines of code, we can % do it

Use mean() function in Matlab to get the average value of an array

Publish Date:2025/04/18 Views:145 Category:MATLAB

This tutorial will discuss finding the mean or average of an array using the function in MATLAB mean() . Use the MATLAB mean() function to find the average of an array To find the mean of an array, we can use Matlab's inbuilt function mean(

Find the index of a value in an array in Matlab

Publish Date:2025/04/18 Views:189 Category:MATLAB

This tutorial discusses find() finding the index of a value in an array using the function in MATLAB. Use the function in MATLAB find() to find the index of a value in an array In an array, elements are placed at certain indices starting fr

Sum of array elements in MATLAB

Publish Date:2025/04/18 Views:153 Category:MATLAB

This tutorial will discuss the use of function in Matlab sum() to find the sum of all the elements in an array. Use the MATLAB sum() function to get the sum of array elements. To get the sum of each element in an array, we can use the built

Plotting Slope Fields in MATLAB

Publish Date:2025/04/18 Views:196 Category:MATLAB

The ODE consists of equations involving functions and their derivatives 常微分方程 . We use 斜率 fields to illustrate the concept of our 微分 equations. We also call slope fields fields direction . Use the function in MATLAB slope_

Plotting the frequency distribution of data in MATLAB

Publish Date:2025/04/18 Views:140 Category:MATLAB

We will study different ways to plot frequency distribution curve of data in MATLAB. We will use different sample codes and relevant outputs to clear your concepts and provide you complete insights using MATLAB. Note that MATLAB allows user

Creating a new graph in Matlab

Publish Date:2025/04/18 Views:192 Category:MATLAB

In this tutorial, we will discuss how to figure() create a new graph using the function in MATLAB. figure() Create a new figure using the MATLAB function If you want to plot data on multiple graphs, you can use figure() the function to crea

MATLAB Maximize Graph

Publish Date:2025/04/18 Views:101 Category:MATLAB

In this tutorial, we will discuss how to use figure() the function in MATLAB to maximize a graph. figure() Maximize graphs using the function in MATLAB If you want to maximize a graph, you can use figure() the maximize function. To maximize

Changing the Properties of Graphics in MATLAB

Publish Date:2025/04/18 Views:129 Category:MATLAB

We will study different ways to change the size, resolution, and background color of the desired figure in MATLAB. We will use different sample codes and relevant output to clear your concepts and give you a comprehensive understanding of t

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial