필터 지우기
필터 지우기

How can i use ' character in a string

조회 수: 31 (최근 30일)
IB Ugur
IB Ugur 2018년 12월 29일
댓글: John D'Errico 2018년 12월 29일
Hi
How can i merge two seperate strings into one string, for instance;
I want to create a variable like that :
>> 'materialproperties/uniaxialelement.txt' which contains ' character inside.
So i use
A=' ' '; B='materialproperties/uniaxialelement.txt'
FD={ A B A};
but it doesnt work
Can u you help me ?
  댓글 수: 1
John D'Errico
John D'Errico 2018년 12월 29일
You were close though. First, note that in order to get a single ' character in a string, you needed to use
A = ' ' ' ';
So a QUADRUPLE ' in there. 3 will give an error. The two on the outside make it a string. Then the two on the inside tell MATLAB it is a ' character.
But then when you tried to create
FD={ A B A};
you also needed to use [] to catenate the strings together. {} creates a cell array.
FD = [ A B A];
Anyway, just use what Stephen gave you, as it is the appropriate solution.

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

채택된 답변

Stephen23
Stephen23 2018년 12월 29일
편집: Stephen23 2018년 12월 29일
How to create character arrays (including single quotes) is described in the documentation:
To get a literal single quote you just need to double it, e.g.:
>> FD = '''materialproperties/uniaxialelement.txt'''
FD = 'materialproperties/uniaxialelement.txt'
Or if you want to define a single quote by itself:
>> A = '''' % one single quote by itself
A = '
>> B = 'materialproperties/uniaxialelement.txt'
B = materialproperties/uniaxialelement.txt
>> FD = [A,B,A] % concatenate
FD = 'materialproperties/uniaxialelement.txt'
  댓글 수: 1
John D'Errico
John D'Errico 2018년 12월 29일
Indeed, this is the first trick you should look for in any similar context. If ' is a special character in context of a string, then consider if just repeating that special character will do something useful.

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

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