Custom fprintf output formatting.

7 ビュー (過去 30 日間)
Kris Glasier
Kris Glasier 2015 年 9 月 30 日
編集済み: John Kelly 2017 年 11 月 10 日
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 件のコメント
per isakson
per isakson 2015 年 10 月 1 日
編集済み: per isakson 2015 年 10 月 1 日
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?
Kris Glasier
Kris Glasier 2015 年 10 月 1 日
編集済み: Kris Glasier 2015 年 10 月 1 日
I'm trying to get the output to give my format in Engineering notation through fprintf. In this case, I'm looking to get it to give Fx, Fy, and Fz as
F = {1.234e567j 12.34e567i 123.4e567k}
Where 567 is a multiple of 3, for the exponent. and the decimal floats to make a value between 1-1000. If you don't know CVN, the i,j, and k represent the x, y, and z components in a 3D Cartesian grid. Essentially I want it to look like the fprintf I have at the end of the function, but instead of it formatting with %.4e, since that gives me a 1.234e5 response, or a value between 1-10 with an exponent n, rather than 3n, if that makes any sense.
All of the individual components work, but being as I put the values in as a character string, all the inputs are in one cell and I can't seem to find a way to tie them all together.

サインインしてコメントする。

回答 (1 件)

Walter Roberson
Walter Roberson 2015 年 10 月 1 日
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.
  1 件のコメント
Kris Glasier
Kris Glasier 2015 年 10 月 1 日
Ah, that did clear a lot of the mess up, Walter. Thank you! However I realize now I have a problem with my values being a string (and only being stored in a 1x1 array). So now my feed, if I put value(1) I simply get the first character (8). Or if I put value(1:5) I get the whole 1.234 value. But if a negative is inserted (ie, an alpha/beta/gamma of >90) the whole set slides over.
Is there a way to turn that 30 odd character string (5-6 characters + 6 spaces between each number set) into a 1x3 or 3x1 array?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by