Is it possible to use zero padding in the variable names when creating a symbolic array of automatically generated elements.
4 次查看(过去 30 天)
显示 更早的评论
I am currently trying to implement an algorithm that generates and solves a set of polynomial equations. I generally need to create R symbolic variables for the polynomials .I wanted to use the sym('a',n) functionality for this. For example:
x_var_list = sym('x%d',[1 R])
Since R can sometimes be 10 or higher, I wanted to introduce zero padding, such that the variables will usually be displayed in sorted order, when printing.
n_digits_R = num2str(ceil(log10(abs(R))))
x_var_list = sym(strcat('x%0',n_digits_R,'d'),[1 R])
This code snippet, however, yields the following error message:
Error using sym>createCharMatrix (line 1561)
If the second argument specifies the dimensions of the generated symbolic vector or matrix, then the first argument must be a character vector specifying the base name for vector or matrix elements. It must be a valid variable name.
Is padding generally incompatible with the sym function?
2 个评论
Vinai Datta Thatiparthi
2020-9-17
Hey Armin,
Is there any specific reason why you use "%0"?
Instead,
sym(strcat('x0',n_digits_R,'d'),[1 R])
% Does this fetch the expected results?
Also, could you mention what are the expected output variable names?
采纳的回答
Walter Roberson
2020-9-17
编辑:Walter Roberson
2020-9-17
No, the code checks that what you pass in is a variable name itself, and if not then it deletes all '%d' and tries again to see if the result is a variable name. There is no possibility in the code for anything other than '%d'
To get around this problem,
syms(compose("x_%02d", 1:R))
or similar variations like
a = sym(compose("x_%02d", 1:R))
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!