How the remove single apostrophe from a string?
조회 수: 9 (최근 30일)
이전 댓글 표시
yfunc=input('Enter any curve equation>') %If input yfunc= 'x^2', how to remove the apostrophe so that I can use the equation...
댓글 수: 0
채택된 답변
Stephen23
2017년 12월 10일
편집: Stephen23
2017년 12월 11일
"how to remove the apostrophe so that I can use the equation..."
You can't, because a char array does not really have single quotes at the ends (unless you put them there), in just the same way the a numeric array does not contain any square brackets at the ends: the single quotes / square brackets are just the syntax that specifies how char arrays / numeric arrays are defined, but are not actually contained within the array itself. Let me demonstrate:
>> char(39) % single quote ASCII code
ans = '
>> double('hello') % get the ASCII codes for |hello|
ans =
104 101 108 108 111
Can you see the code value 39 anywhere in that list?
In any case, if you have a char array then there is nothing that can be removed that will magically turn that char array into a function. A char array is just a collection of characters, which have no meaning other than any semantic meaning from how they were defined. If those characters happen to form a function in some variable then you will need to convert it into a function using str2func, I have shown you how below:
>> str = input('Enter function of x: ','s');
Enter function of x: x.^2+x
>> fun = str2func(sprintf('@(x)%s',str));
>> fun(0:10)
ans =
0 2 6 12 20 30 42 56 72 90 110
Note that this will work as long as the function is entered using MATLAB syntax. If you want to define your own syntax then you will have to write a language parser using MATLAB, which is not a trivial task.
You really need to revise basic MATLAB types, such as what are char arrays and functions, because otherwise using MATLAB will not make sense:
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Characters and Strings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!