add character in a certain position in character array-matlab2015b

조회 수: 24 (최근 30일)
Maria K
Maria K 2017년 10월 16일
댓글: Maria K 2017년 10월 16일
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!

채택된 답변

Walter Roberson
Walter Roberson 2017년 10월 16일
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
Walter Roberson
Walter Roberson 2017년 10월 16일
random_rgb = colors( randi(size(colors,1)), : );
Maria K
Maria K 2017년 10월 16일
It worked perfectly. Thanks for your time!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by