How do I enter same string input in a vector?
이전 댓글 표시
I am trying to create a vector (not a string array) with repeated string entries such as "30 T" for a specific length.
for i = 1:879
S = Contour1(i);
Sy = S.Lon
Sx = S.Lat
UTMZ = zeros(length(Sy),1);
UTMZ(:)= ['30 T'];
GeoDeg = utm2deg(Sx',Sy',UTMZ)
plot(Sx,Sy)
i;
end
After creating a vector, I can't seem to have the entry repeat itself. I can't also manually entre the values in each vector because the length of the vector varies for each loop.
Any sugestions on how to entre a repeating string within a vector?
답변 (1개)
Walter Roberson
2022년 7월 26일
UTMZ = repmat('30 T', 1, length(Sy)).';
댓글 수: 4
Charlotte Wargniez
2022년 7월 27일
편집: Charlotte Wargniez
2022년 7월 27일
Walter Roberson
2022년 7월 27일
Yes, that is what you asked for. You specified that a string() array is not to be created, and you asked that a vector be created.
MATLAB has a small number of ways to deal with text:
- in cases where each group of text is to be treated as a single item, such as wanting UTMZ(7) to refer to text 30 T then using a string array is recommended
- in cases where individual characters need to be directly addressable and you are dealing with exactly one text group, use a row vector of char
- in cases where you have a 2d array of text of fixed size, and need to address each character directly, such as a Battleships map, use a 2d array of char
- in cases where you have text groups of different size, perhaps a row or column vector of such groups, and you do not wish to use a string() array, use a cell array of character vectors.
UTMZ = repmat({'30 T'}, length(Sy), 1);
The individual cells can be examined and changed such as UTMZ{7} = '18 T';
Charlotte Wargniez
2022년 7월 27일
편집: Charlotte Wargniez
2022년 7월 27일
Walter Roberson
2022년 7월 28일
I found that function in the File Exchange. It is an odd function -- it requires that the third parameter be a 2d array of character with four columns and the letter codes have to be in the 4th column.
The particular case of a 2D array of char is the one I referred to in the line about a Battleships map. The technique you used with repmat of a cell array and char() that, is fine, but you could also be more efficient as
UTMZ = repmat('17 T', length(Sy), 1);
카테고리
도움말 센터 및 File Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!