Replace String in Character Array with complete Character Array (100x100)

조회 수: 3 (최근 30일)
Daniel Rohrer
Daniel Rohrer 2020년 5월 17일
댓글: Daniel Rohrer 2020년 5월 18일
I have following string in a 1x287000 character array:
I would like to replace the String "coordIndex" with a complete 100x100 character array.
With strrep I have to specify the string I'd like to insert:
new_str = strrep(new_str,'coordIndex','something_else');
I tried this but that doesn't work obviously (u is the 100x100 character array):
new_str = strrep(new_str,'coordIndex', u );
I couldn't find any solution to this problem so far.

답변 (1개)

Image Analyst
Image Analyst 2020년 5월 17일
You cannot insert a 100x100 2-D matrix into a 1x287000 row vector.
You can only insert it into a matrix that has at least 100 rows, not into a vector of only a single row. I mean, where would the lower 99 rows go???
You can turn that 100x100 into a row vector also and then insert it. Would that be acceptable?
output = [vec1(1:index), vec2(:)', vec1(index+1, end)];
index is the index after which you wish to insert the other elements. Would the above be acceptable?
  댓글 수: 3
Image Analyst
Image Analyst 2020년 5월 17일
편집: Image Analyst 2020년 5월 17일
You need your vector, and your 2-D matrix. What are the names of those two variables? I just assumed they were vec1 and vec2. Replace them with whatever your names are. You also have to define coordIndex. Maybe try
coordIndex = [0, 1, 2, 3, -1;...
4, 5, 6, 7, -1];
index = strfind(vec1, coordIndex(:)');
Daniel Rohrer
Daniel Rohrer 2020년 5월 18일
My vector and matrix are named as following:
I defined now the coordIndex like this (extracted from new_str):
coordIndex = extractBetween(new_str,"coordIndex [","]");
Now the code should look like this:
index = strfind(new_str, coordIndex(:)');
With this my index is basically empty:
------------------------------------------------------------------------------------------------------------------------------------
I have another issue with the creation of the vector from the matrix, yesterday it somehow worked, but now I cleared the workspace and loaded it newly, but the vector is not what I intend:
output = u(:).';
I tried following code but same result:
n = size(u);
output = reshape(u,[1,(n(1:1)*n(2:2))]);

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

카테고리

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

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by