How to get the 1×1 cell array?
1 view (last 30 days)
Show older comments
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 Comment
Stephen23
on 3 Jan 2023
Edited: Stephen23
on 3 Jan 2023
The best approach is to either use SPRINTF, just as Voss shows here:
https://www.mathworks.com/matlabcentral/answers/1887597-how-to-get-the-1x1-cell-array#answer_1140062
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"
Accepted Answer
KSSV
on 3 Jan 2023
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
More Answers (1)
Voss
on 3 Jan 2023
offset = -20;
pos = 10;
neg = 5;
str = {sprintf('m, %g, %g, L -%g, 0 c 0, 0',offset+pos,offset+neg,neg-0.3)}
See Also
Categories
Find more on Whos in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!