Custom fprintf output formatting.

7 Ansichten (letzte 30 Tage)
Kris Glasier
Kris Glasier am 30 Sep. 2015
Bearbeitet: John Kelly am 10 Nov. 2017
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 Kommentare
per isakson
per isakson am 1 Okt. 2015
Bearbeitet: per isakson am 1 Okt. 2015
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 am 1 Okt. 2015
Bearbeitet: Kris Glasier am 1 Okt. 2015
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.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Walter Roberson
Walter Roberson am 1 Okt. 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.
  1 Kommentar
Kris Glasier
Kris Glasier am 1 Okt. 2015
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?

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by