Filter löschen
Filter löschen

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

3 Ansichten (letzte 30 Tage)
Dmitriy Saltanov
Dmitriy Saltanov am 17 Jun. 2021
Kommentiert: Stephen23 am 14 Apr. 2024
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 =
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?

Antworten (2)

Arun am 14 Apr. 2024
Bearbeitet: Arun am 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
{'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 am 14 Apr. 2024
compose with symbolic parameters is the function composition operation, not an operation for producing text





Community Treasure Hunt

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

Start Hunting!

Translated by