Updated February 8, 2023
Introduction to Cell to String MATLAB
There are two commands used to covet cell data into string format one is char and the other is a string. char and string commands extract all the data from cell arrays and stored in the form of string. In Matlab, we use string notations as data in single or double quotes ( “ ” or ‘ ‘ ). There are some operations and methodologies that cannot be operated on cells or cell arrays but can be operated on strings, in such cases this manipulation is used. There is no limitation to the cell data, it can be a single element or in the form of vectors or in form of multidimensional matrix.
Syntax:
Out = char ( input)
- Output variable name = char ( input cell name)
Out = string ( input)
- Output variable name = string ( input cell name)
How does Cell to String Matlab work?
To convert cell data into a string first we need to create cells with some data .cell is created by using curly brackets ( { } ). If data is single-dimensional then elements can be separated by a comma and if data is multi-dimensional then arrays are separated by a semicolon (;) along with elements separated by a comma. After assigning cell we can apply command char or string by using the above syntax format to convert all the data into the string.
Examples to Implement Cell to String MATLAB
Below are the examples mentioned:
Example #1
In the first example let us assume one input cell as a variable input. One data is assigned to the input which is ‘hello’. At the output side, we can check data is in string format.
Code:
clc ;
clear all ;
input = {'hello'}
out = char(input)
Output:
Example #2
In this example we have assigned colors name array to input, that means in this problem input is cell array or vector which has multiple values in a single dimension. Input array is { ‘ red ‘ , ‘ blue ‘ , ‘ yellow ‘ , ‘ green ‘ , ‘ black ‘ , ‘ white ‘ } .after applying command we will get output as independent and separate strings of input .
Code:
clc ;
clear all ;
input = { ' red ' , ' blue ' , ' yellow ' , ' green ' , ' black ' , ' white ' }
str = char ( input )
Output:
Code:
clc ;
clear all ;
input = {' 3 ' , ' 5 6 ' , ' 4 3 ' , ' 2 2 ' , ' 5 4 ' }
out = char ( input )
Output:
Example #3
In this example let us consider input array is a multidimensional matrix with three rows and three columns. input is cell-matrix along with input data in form of alphabets such as { ‘ G ‘ , ‘ F ‘ , ‘ E ‘ ; ‘ R ‘ , ‘ D ‘ , ‘ N ‘ ; ‘ V ‘ , ‘ S ‘ , ‘ C ‘ }
Code:
clc ;
clear all ;
input = { ' G ' , ' F ' , ' E ' ; ' R ' , ' D ' , ' N ' ; ' V ' , ' S ' , ' C ' }
out = char ( input )
Output:
Example #4
Now let us consider input in form of all the data types such as int, float, char, and string. in this example input is assigned with a single dimensional array or vector with data ‘ HELLO ‘, ‘ 356 ‘, ‘ HI ‘, ‘ 22.5 ‘,’ B Y E ‘, ‘ 10 ‘ }. in this HELLO, HI and BYE are string datatype. 365 and 10 are integers and 22.5 is float.
Code:
clc ;
clear all ;
input = {' HELLO ' , ' 356 ' , ' HI ' , ' 22.5 ' ,' B Y E ' , ' 10 ' }
out = char( input )
out ( 3 )
out(5)
Output:
Conclusion
In this article, we have seen how to convert cell data into the string by using string function and char function. We can convert all types of data integer, character, float, etc into the string format. As well as we can convert all formats (single element, one-dimensional array, multidimensional array) of data into the required format.
Recommended Articles
This is a guide to Cell to String MATLAB. Here we discuss an introduction to Cell to String MATLAB, syntax, how does it work with examples respectively. You can also go through our other related articles to learn more –