How do I force the output format of arrayfun() to be "short"?

2 次查看(过去 30 天)
How do I force the output format of arrayfun() to be "short"?
The code and results to illustrate my issue:
format; %this is the default matlab output format I think
axlim = [ 0.1 50; 0.1 7; 50 2000]
format shortG;
axlim
lbl1 = arrayfun(@(lo,hi) sprintf('(%d,%d)', lo, hi), axlim(:,1), axlim(:,2), 'UniformOutput', 0)
axlim =
1.0e+03 *
0.0001 0.0500
0.0001 0.0070
0.0500 2.0000
axlim =
0.1 50
0.1 7
50 2000
lbl1 =
'(1.000000e-01,50)'
'(1.000000e-01,7)'
'(50,2000)'
The result that I need is:
lbl1 =
'(0.1 ,50)'
'(0.1,7)'
'(50,2000)'
Kind of related to this https://www.mathworks.com/matlabcentral/newsreader/view_thread/156758 but I need an extra step of forcing the output format to be simple and short

采纳的回答

Adam
Adam 2017-1-20
编辑:Adam 2017-1-20
lbl1 = arrayfun(@(lo,hi) sprintf('(%g,%g)', lo, hi), axlim(:,1), axlim(:,2), 'UniformOutput', 0)
The help...
doc sprintf
contains a section on the formatSpec and the different conversions you can use. %g is the compact version.
It is totally independent of the format you are using - it is entirely dictated by what is in your sprintf since it is a string that you are outputting.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by