It is not possible to use the fprintf format specifier directly for the required format. The method is to use the output of disp as a string for printing. But disp does not return a string, it writes directly to standard output. So how to do this?
Here evalc (eval with capturing output) comes to the rescue:
%// Create helper function sdisp = @(x) strtrim(evalc(sprintf('disp(%g)', x))); %// Test helper function format ShortEng; a = 123e-12; fprintf(1, 'Test: %s', sdisp(a));
This workaround, of course, can have unpleasant consequences in many respects due to unverified inputs of auxiliary functions. But this illustrates the point and is one of the rare cases where the offended family of eval functions is virtually indispensable.
user2271770
source share