필터 지우기
필터 지우기

Create an array of strings

조회 수: 31 (최근 30일)
Viesturs Veckalns
Viesturs Veckalns 2017년 10월 21일
편집: Viesturs Veckalns 2017년 10월 21일
I try to create an array of strings
fruit = ['apple', 'cherry']
What modifications are needed so that fruit(2) is 'cherry' not 'p'?
UPDATE
Following the two first answers to this question I created a case:
style = {'.blue', '.red'};
f1 = figure;
for theta = (0:0.1:1)*pi
for ind = 1:2
figure(f1);
plot(theta, cos(theta + pi*ind), style(ind)); hold on;
end
end
The code does not accept style(ind) as an argument. The error message is
Error using plot
Invalid first data argument
  댓글 수: 2
per isakson
per isakson 2017년 10월 21일
편집: per isakson 2017년 10월 21일
"does not accept style(ind) as an argument" That's because it is a cell array of strings, not a character string. I guess the error message said so.
Replace
style(ind)
by
style{ind}
with curly braces.
Viesturs Veckalns
Viesturs Veckalns 2017년 10월 21일
That worked

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

채택된 답변

per isakson
per isakson 2017년 10월 21일
편집: per isakson 2017년 10월 21일
>> fruit = {'apple', 'cherry'}
fruit =
'apple' 'cherry'
with curly braces creates a cell array of strings. I use R2016a. The display format for cell arrays has been changed. And to reference one of the strings
>> fruit{2}
ans =
cherry
See Create Cell Array. Note the links at the bottom of the page.

추가 답변 (1개)

Birdman
Birdman 2017년 10월 21일
fruit = ['apple,cherry'];
fruit=strsplit(fruit,',');
disp(fruit(2))

카테고리

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