Prev: Exporting matrix from workspace to edit eqaution (Mathtype)
Next: Downloading Bloomberg data using BSID
From: jens on 28 May 2010 02:11 I have a cellarray that both contains strings and numeric data. Example: cellarray={'Milk' '3.51e+015' 3.51e+07 'Cheese' 3.51e+15} sprintf('%s\n',cellarray{:}) I get: Warning: The argument for the %s format specifier must be of type char (a string). ans = Milk 3.51e+015 Cheese 3.510000e+015 For some reason it will not write all numeric data? Jens
From: David Young on 28 May 2010 03:51 The %s format looks for a string to print, but the third element of your cell array is a double, not a string, so you get a warning and it isn't printed. Possible solutions: 1. Is there a good reason to mix strings and numbers in your cell array in the first place? Maybe your program would be better structured if you didn't, and you'd not only solve this problem but also avoid other pitfalls later. 2. If the strings and numbers form a regular pattern in the cell array, you can adjust the format specifier to take account of this. For example, if every third element is a number, the following would work: sprintf('%s\n%s\n%g\n', cellarray{:}) and as it happens this works on your example. 3. If you really have to have numbers and strings mixed unpredictably, you could convert each number to a string using num2str or sprintf applied to individual elements of the cell array, then concatenate the strings. Go for solution 1 if you can.
From: David Young on 28 May 2010 04:09 Sorry - a slight correction to what I said above. I've just noticed that the 5th element of your cell array is also a number, not a string, so my example format should not print it. My suggestions still stand, though. However, the fact that it is printed is quite strange. On further investigation I get this (in 2010a) >> sprintf('%s\n', 3e8) Warning: The argument for the %s format specifier must be of type char (a string). ans = >> sprintf('%s\n', 3e9) ans = 3.000000e+009 Does anyone know how this behaviour arises? It seems inconsistent - could it be a bug?
From: TideMan on 28 May 2010 05:56
On May 28, 8:09 pm, "David Young" <d.s.young.notthis...(a)sussex.ac.uk> wrote: > Sorry - a slight correction to what I said above. I've just noticed that the 5th element of your cell array is also a number, not a string, so my example format should not print it. My suggestions still stand, though. > > However, the fact that it is printed is quite strange. On further investigation I get this (in 2010a) > > >> sprintf('%s\n', 3e8) > Warning: The argument for the %s format specifier must be > of type char (a string). > ans = > > >> sprintf('%s\n', 3e9) > ans = > 3.000000e+009 > > Does anyone know how this behaviour arises? It seems inconsistent - could it be a bug? It's a feature not only of 2010a, but also 2006a, so it's been around for a while. And it seems to depend in some way on the significand: >> sprintf('%s\n', 1e9) Warning: The argument for the %s format specifier must be of type char (a string). >> sprintf('%s\n', 8e9) ans = 8.000000e+009 |