Custom fprintf output formatting.
7 views (last 30 days)
Show older comments
I'm currently trying to build a function that will enable myself to have an output in Engineering Notation within fprintf. The following script is supposed to resolve a 3D force vector into it's i/j/k format in Eng Notation.
function [] = ABGresolve(force,alpha,beta,gamma)
Fx = force*cosd(alpha); % i
Fy = force*cosd(beta) ; % j
Fz = force*cosd(gamma); % k
val = [Fx Fy Fz] ;% Force vector
% convert exponent to *10^(3(orderOfMag))
% so that, for instance, x^4 would be 10^3(1)
tempVal = abs(val) ;% removes negatives for log10
orderOfMag=floor(log10(tempVal)./3);
if val == 0
orderOfMag = 3 ;% can't log a value of 0, so....
end
%Here you can change the number of digits to the left
% of the decimal. fiddle to understand it.
offset=0-orderOfMag;
% Divides original value by
% power of 3n to get value between 1-1000
valueToPrint=val./(10.^(3.*orderOfMag));
valueToPrint = val.*10.^offset; % % Old stuff I don't use
precision = 4 ;% number of display digits
expPrecision = 3 ;% number of digits in the exponent
if orderOfMag > 0
formatStr = ['%0' num2str(expPrecision) 'i'];
else
formatStr = ['-%0' num2str(expPrecision) 'i'];
end
value = num2str(valueToPrint,precision);
exponent = [formatStr,abs((orderOfMag).*3)];
your_string = [ value(1:6), 'e', sprintf(formatStr,exponent(:)) ...
value(2),'e', sprintf(formatStr,exponent(:)) ...
value(3),'e', sprintf(formatStr,exponent(:))]
fprintf('F ={ %.4ei %.4ej %.4ek}\n',Fx,Fy,Fz);
end
When I try to format the components, I get a bunch of gibberish in the output. I need to convert my character string to a [1x3] or a [3x1] array and somehow get the exponent to work as a multiple of 3. This is my current output:
86.6 e-6e-.e-
I was trying to adapt this Answer on a very similar question to do multiple elements withing a single vector ( Val = [Fx Fy Fx] ) however I don't know where my errors are.
I started learning how to use the R2010a version of MATLAB only a couple weeks ago in my Programming class, so my understanding is rather limited in regards to the built in functions.
Thanks in advance.
Edit: Updating my function in regards to Walter's realization that I was double-formatting a string.
4 Comments
per isakson
on 1 Oct 2015
Edited: per isakson
on 1 Oct 2015
- Keep it simple!
- See Debug a MATLAB Program
and
K>> class(value)
ans =
char
K>> formatStr
formatStr =
-%03i
K>> class( exponent )
ans =
char
K>> sprintf(formatStr,exponent(:))
ans =
-045-037-048-051-105-003-003-003
What should the output look like?
Answers (1)
Walter Roberson
on 1 Oct 2015
You use sprintf(formatStr,exponent(:)) after having done
exponent = [formatStr,abs((orderOfMag).*3)];
so your exponent() array starts with your formatStr and that is being converted using your format string.
See Also
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!