필터 지우기
필터 지우기

How do I get an element from a string?

조회 수: 9 (최근 30일)
Kratos
Kratos 2015년 2월 21일
편집: Kratos 2015년 2월 21일
Say I have a vector representing a row and column which is (5,6) and I have a
vec = (5,6);
string = ['oNTitsr
reaqBtr
sggtalu
imnvomo
nresgLm
eamhomt
lfHoan
sn mnin']
When I do
a = string(vec);
I get a = 'ne'
and when I do
a = string(5,6);
a = 'L'
I am suppose to get 'L' but I keep getting 'ne' when I run my code.
vec = (5,6) is just an example it can be anything. How do I do it?
  댓글 수: 2
Andrew Newell
Andrew Newell 2015년 2월 21일
Is that really the code you used? It should produce errors right from the start. The line
vec = (5,6);
results in
Error: Expression or statement is incorrect--possibly unbalanced (, {, or [.
The string definition you used gives
Error: String is not terminated properly.
Kratos
Kratos 2015년 2월 21일
편집: Kratos 2015년 2월 21일
It was suppose to be vec = [5,6]

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

채택된 답변

Andrew Newell
Andrew Newell 2015년 2월 21일
편집: Andrew Newell 2015년 2월 21일
Here is some code that works:
vec = {5,6};
S = ['oNTitsr';
'reaqBtr';
'sggtalu';
'imnvomo';
'nresgLm';
'eamhomt';
' lfHoan';
'sn mnin'];
S(5,6)
S(vec{:})
Some features of this code that are needed:
  1. Each row of the char array is surrounded by quotes; semicolons makes it a column vector.
  2. They all have to have the same length - hence the space at the beginning of ' lfHoan'.
  3. A cell array is used for vec. Using {:} returns a list:
vec{:}
ans =
5
ans =
6

추가 답변 (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