See the link above about handling strings.

]]>title("the value is "+number)

@Julian-

So nice to hear that this saves you a lot of code and some hassle! Thanks for the illustration.

@Rob-

Please put in an enhancement request via tech support for your thoughts on the tick labels for the colorbar. In the meantime, at least you have a relatively simple way to accomplish your goal. Thanks!

@Stuart- Sure you can use string (not strings):

>> nums = 1:5 nums = 1 2 3 4 5 >> string(nums) ans = 1×5 string array "1" "2" "3" "4" "5"]]>

Cool.

If your numbers are only integers (and not reals) , I assume you can also use:

>>nums=1:5;

>>strings(nums)

But another thing I do a lot is plot 2D data using imagesc, but plot log10 of the data. I then add a colorbar and it gives me ticks of powers of 10… which is never useful. So I always have to write extra code to make nice tick labels of 10^{something}.

Matlab – make this a default feature! A colorbar for a log10 (or log) dataset!

However, with compose I can speed up (and reduce the lines) of the code I need to do my log10 colorbar ticks labels.

e.g.

%% Draw some random data, and plot log10 of it

figure(1)

x = rand(10,10);

% I want a log10 plot

imagesc(log10(x))

% Add a colorbar, and give it a range from 0.001 to 1 (prior to the log10’ing’)

h = colorbar;

set(gca,’CLIM’,log10([0.001, 1]))

set(h,’Ticks’,-3:0) % -3 is log10(0.001), etc.

%% Make the ticks the values I want them to be, using compose!

set(h,’TickLabels’, compose(’10^{%d}’,get(h,’Ticks’)))

Done. That’s neater than what I was using before! (Also, 1 line.)

]]>It’s really useful for constructing a series of labels which could include text and numbers. You might want these labels in plots (for axis-labels or datatips), or you could want them as RowNames in a table or category names for a categorical. compose provides a natural way to do this. Prior to the arrival of strings and compose I used to have constructs like the example below appearing all too frequently in my code.

>> a = 1:4; >> [{} arrayfun(@(x)sprintf('Example %d',x), a, 'uniformoutput', false)] ans = 1×4 cell array {'Example 1'} {'Example 2'} {'Example 3'} {'Example 4'}

Indeed 10 years ago I made a function to create sets of labels discrete spaces over several sets of axes strprod (string product) that worked on cellstr arrays

>> strprod({'amps ' 'volts '}, {'motor1' 'motor2'}, {' mean' ' min' ' max'}) 2×2×3 cell array ans(:,:,1) = {'amps motor1 mean' } {'amps motor2 mean' } {'volts motor1 mean'} {'volts motor2 mean'} ans(:,:,2) = {'amps motor1 min' } {'amps motor2 min' } {'volts motor1 min'} {'volts motor2 min'} ans(:,:,3) = {'amps motor1 max' } {'amps motor2 max' } {'volts motor1 max'} {'volts motor2 max'}

Nowadays I can use a string expression to create the same space

>> ["amps " "volts "]' + ["motor"+(1:2)+" "] + permute(["mean" "min" "max"], [3 1 2]) 2×2×3 string array ans(:,:,1) = "amps motor1 mean" "amps motor2 mean" "volts motor1 mean" "volts motor2 mean" ans(:,:,2) = "amps motor1 min" "amps motor2 min" "volts motor1 min" "volts motor2 min" ans(:,:,3) = "amps motor1 max" "amps motor2 max" "volts motor1 max" "volts motor2 max" >>]]>

So what would be easier for me is to allow me to put in numeric values into a text function such as [ ] or one on the concatenate commands. If the variable type was numeric and it was going into a string function, MATLAB would automatically use num2string so that I can just type the variable name and MATLAB will do they type conversion from numeric (typically the usual double precision) to text. I’m not against compose, but I don’t see how it provides benefit beyond num2str. ]]>

Thanks for the snippet. You can control N by generating a large enough collection by increasing N_rand – but it’s not deterministic hope my tries. Just keep adding more attempts until you reach N suitable ones.

–loren

]]>N_rand = 1000; x_rand = rand(1, N_rand) - 0.5; y_rand = rand(1, N_rand) - 0.5; r_rand = sqrt(x_rand.^2 + y_rand.^2); ix = r_rand <= 0.5; cos_rand = x_rand(ix)./r_rand(ix); sin_rand = y_rand(ix)./r_rand(ix); N = sum(ix);

The only problem that you will not control N.

Daniel

]]>OK, thanks for the update!

]]>