add character in a certain position in character array-matlab2015b

Hello! Is there anyway I can add a new character in a certain position of a character array? For example I have 'ABCD' and i want to create a new array that has 'ABCTD'. I am using Matlab 2015b so the insertAfter command won't work... Thanks on advance!

 Réponse acceptée

For string S and insertion at position N+1 (that is, after position N), then
[S(1:N), NewText, S(N+1:end)]
There is no function provided for this purpose, but you can create one easily. You might want to consider generalizing to allow inserting a new column into a character array (the above is for a character vector), perhaps offering the special case of inserting the same character for every row if the replacement is not a column. Perhaps,
function NewCharArray = insertAfter( CharArray, Position, WhatToInsert)
NewCharArray = char( strcat( cellstr(CharArray(:,1:Position)), cellstr(WhatToInsert), cellstr(CharArray(:, Position+1:end)) ) );

6 commentaires

Maria K
Maria K le 16 Oct 2017
Modifié(e) : Maria K le 16 Oct 2017
Is it possible to make variable the color of the string letter through this function? Thanks a lot!
No, color or font or font angle or font size are not attributes of characters. Those are attributes of displaying characters. The method to add colors to display of characters depends upon how you are displaying them.
For example if you needed to display to the command window, then you could use Yair's cprintf from the File Exchange, which can display in Black, Cyan, Magenta, Blue, Green, Red, Yellow, or White.
Hello again mr Roberson. I have one more question... Say i have a variable called 'colors' with red and blue in rgb
colors = [1 0 0; 0 0 1 ]
I also have a letter 'A' and i want it to appear either in red or in blue. How can I choose one of the colors in random?
random_rgb = colors( randi(size(colors,1)), : );
It worked perfectly. Thanks for your time!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Characters and Strings dans Centre d'aide et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by