h = 
Why Does sym(cell) Not Return a Cell?
显示 更早的评论
As far as I can tell, there is no documented behavior for calling sym with single input that's a cell. Nevertheless, if doing so doesn't result in an error, then I'd expect it to return a cell, but it doesn't.
syms a
c = {a,a}
h = sym(c), iscell(h)
c = {pi,sqrt(2)}
h = sym(c),iscell(h)
I guess I would expect sym(c) to be shorthand for
h = cellfun(@(c) sym(c),c,'Uni',false)
iscell(h)
5 个评论
Torsten
2026-9-6,20:40
Answer from AI:
A symbolic object itself is not a cell array, but you can store symbolic objects inside a standard MATLAB cell array, or convert between cell arrays and symbolic arrays. [1, 2, 3]
Key Facts
- Data Types: A symbolic object (sym or symfun) has its own distinct class and is not a cell array by default. [1]
- Storage: You can place multiple symbolic variables, expressions, or mixed symbolic functions into a cell array using curly braces {}. This is required if you want an array of different symbolic functions, because regular parentheses () indexing on symbolic functions triggers function evaluation rather than indexing. [1]
- Conversion Functions:
- Use cell2sym to convert a cell array containing valid elements into a symbolic array.
- Use sym2cell to convert a symbolic array into a standard cell array. [1, 2]
Nevertheless, if doing so doesn't result in an error, then I'd expect it to return a cell, but it doesn't.
Why would you expect that?
c = {'apple', 'banana'}
s = string(c)
Should s be a cell array? No, it shouldn't. Now if a class defines a cell conversion method that could work. But not a lot of classes define such a conversion method. Usually users want to contain an object or array in a cell rather than converting an object or array into a cell, and for that you want to use the {} operator or perhaps mat2cell or num2cell.
c2 = {s}
c3 = mat2cell(s, 1, [1 1])
c4 = num2cell(s)
Paul
2026-9-7,20:39
I'm not 100% certain (I'd have to go back and check) but I suspect the sym(cell) syntax was introduced in order to allow creating a vector of symbolic variables in one call. Back before string arrays were introduced, in order to store two or more pieces of text data in one variable you had two options: a char matrix or a cell array each element of which is a char vector (commonly called a cellstr, see the cellstr and iscellstr functions).
dataChar = ['apple '; 'banana'] % Note the space after apple
dataCell = cellstr(dataChar)
Passing either dataChar or dataCell into sym creates a 2-by-1 sym array, as a generalization of the sym("x") and sym(symnum) syntaxes listed in the documentation.
sChar = sym(dataChar)
sCell = sym(dataCell)
whos sChar sCell
Though today, since string arrays exist, we strongly recommend using those instead of char matrices (which require padding the char vectors with spaces to make them the same length) or cellstrs. See this documentation page for more information.
Iit appears it's an undocumented feature for backwards compatibility using modern syntax internally,
dbtype sym 1279:1281
...code for handling other classes of input not shown for brevity... dpb
dbtype sym 1310:1316
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Conversion Between Symbolic and Numeric 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!