필터 지우기
필터 지우기

How to get the 1×1 cell array?

조회 수: 3 (최근 30일)
Smithy
Smithy 2023년 1월 3일
편집: Stephen23 2023년 1월 3일
hello, everybody
I would like to get the 1×1 cell array of {'m, -10, -15, L -4.7, 0 c 0, 0'}.
some values are caluclated from the variables.
I tried with str = {'m, offset+pos, offset+neg, L -neg+0.3, 0 c 0, 0'}.
However, the answer is {'m, offset+pos, offset+neg, L -neg+0.3, 0 c 0, 0'}.
it is just string and no calculation of variables.
I also tried with str = {"m", offset+pos, offset+neg, "L" -neg+0.3, 0 "c" 0, 0};
There is calculation of variables, However, it is 1×9 cell array.
How to get the 1×1 cell array of {'m, -10, -15, L -4.7, 0 c 0, 0'}?
offset = -20;
pos = 10;
neg = 5;
% str = {'m, offset+pos, offset+neg, L -neg+0.3, 0 c 0, 0'}; % just string and no calculation of variables
% str = {"m", offset+pos, offset+neg, "L" -neg+0.3, 0 "c" 0, 0}; % it is 1×9 cell array
% output I want is : 1×1 cell array of {'m, -10, -15, L -4.7, 0 c 0, 0'}
% str = {'m, -10, -15, L -4.7, 0 c 0, 0'};
  댓글 수: 1
Stephen23
Stephen23 2023년 1월 3일
편집: Stephen23 2023년 1월 3일
The best approach is to either use SPRINTF, just as Voss shows here:
or the new overloaded STRING operators, e.g.:
offset = -20;
pos = 10;
neg = 5;
str = "m, "+(offset+pos)+","+(offset+neg)+" L "+(-neg+0.3)+", 0 c 0, 0"
str = "m, -10,-15 L -4.7, 0 c 0, 0"

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

채택된 답변

KSSV
KSSV 2023년 1월 3일
offset = -20;
pos = 10;
neg = 5;
str = {['m, ', num2str(offset+pos),',', num2str(offset+neg), ' L ', num2str(-neg+0.3),',', '0 c 0, 0']}; % just string and no calculation of variables
str
str = 1×1 cell array
{'m, -10,-15 L -4.7,0 c 0, 0'}
  댓글 수: 1
Smithy
Smithy 2023년 1월 3일
Wow.. Thank you very much~!!! It works really well. I really really appreciate with it.

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

추가 답변 (1개)

Voss
Voss 2023년 1월 3일
offset = -20;
pos = 10;
neg = 5;
str = {sprintf('m, %g, %g, L -%g, 0 c 0, 0',offset+pos,offset+neg,neg-0.3)}
str = 1×1 cell array
{'m, -10, -15, L -4.7, 0 c 0, 0'}
  댓글 수: 1
Smithy
Smithy 2023년 1월 3일
Thank you very much. It works well. Wonderful.

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

제품


릴리스

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by