How could i make a vector with char
Show older comments
I need to create the following vector
First I use
syms A B 3D E F G H
V3=[A,B,3D,E,F,G,H,2,58]
It seems that im having a mistake while using syms, how can i fix it if i want my vector like this;
V3= A B 3D E F G H 2 58;
Answers (1)
Walter Roberson
on 20 Jan 2018
syms is not for declaring characters. syms is for creating symbolic variables. All symbolic variable names must be valid MATLAB variable names, so 3D is not a valid symbolic variable name.
It is not possible to create a vector of char in which some of the elements are multiple characters and some of them are single characters.
You can create a cell array:
V3 = {'A', 'B', '3D', 'E', 'F', 'G', 'H', '2', '58'};
In current releases, by default this would display as
1×9 cell array
{'A'} {'B'} {'3D'} {'E'} {'F'} {'G'} {'H'} {'2'} {'58'}
but if you specifically ask disp(V3)
'A' 'B' '3D' 'E' 'F' 'G' 'H' '2' '58'
With R2017a and later, you can create a string() vector:
V3 = ["A" "B" "3D" "E" "F" "G" "H" "2" "58"];
by default this would display as
1×9 string array
"A" "B" "3D" "E" "F" "G" "H" "2" "58"
if you disp() it then you get the same except without the "1 x 9 string array" part.
Are you trying to produce the string 'A B 3D E F G H 2 58' from a cell array of character vectors, {'A', 'B', '3D', 'E', 'F', 'G', 'H', '2', '58'} ? If so then strjoin() the cell array with ' ' as the second argument
V3 = strjoin({'A', 'B', '3D', 'E', 'F', 'G', 'H', '2', '58'}, ' ')
Categories
Find more on Operations on Strings in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!