Textualizing VPA array with compose() seems to be broken?

2 views (last 30 days)
Hi, I'm trying to convert an array of vpa numbers into strings using %-formats with compose() and it gives me an error, like this:
compose("%.12f", [vpa(1/3);vpa(1/11)])
->
Error using sym>convertChar (line 1580)
Character vectors and strings in the first argument can only specify a variable or number. To evaluate character vectors and strings representing symbolic expressions, use 'str2sym'.
Error in sym>tomupad (line 1296)
S = convertChar(x);
Error in sym (line 234)
S.s = tomupad(x);
Error in sym/compose (line 47)
f = sym(f);
To make sure the format I need is supported, sprintf does the job albeit not the one that I need of course:
sprintf("%.12f", [vpa(1/3);vpa(1/11)])
->
ans =
"0.3333333333330.090909090909"
So my question is if there is a workaround for a straightforward conversion of a vpa array to a formatted text array, by which I mean no additional checks and conversions/casts?

Answers (2)

Arun
Arun on 14 Apr 2024
Edited: Arun on 14 Apr 2024
Hi Dmitriy,
I understand that you want to convert a VPA array to a formatted text array. However, using the output of the “vap” function as an input to the “compose” function generates an error.
The “vap” function produces output of “sym” type and “compose” function does not support “sym” type as an input which leads to the error. The “compose” function supports inputs of numeric, character, or string array types, specified as scalar, vector, matrix or multidimensional array. Therefore, the issue does not indicate that “compose()” function is broken.
As a possible workaround, refer the following code to convert a VPA array into a formatted text array, which may prove helpful:
% Example VPA array
vpaArray = vpa([1/3, 1/11]);
% Convert VPA array to a formatted text array
textArray = arrayfun(@(x) sprintf('this is the required value: %.12f', x), vpaArray, 'UniformOutput', false);
%display result
disp(textArray);
{'this is the required value: 0.333333333333'} {'this is the required value: 0.090909090909'}
Please refer to the following shared MATLAB documentation links for more information:
Hope this helps.

Walter Roberson
Walter Roberson on 14 Apr 2024
compose with symbolic parameters is the function composition operation, not an operation for producing text

Tags

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!