0

I have the following:

a = 

{1x1 cell}    {1x1 cell}    {1x1 cell}    {1x1 cell} 

where:

a{:}

ans = 

'a'


ans = 

'a'


ans = 

'c'


ans = 

'a'

I want to have the characters: a a c a

Since I need the characters to print using fprintf

fprintf won't accept a{:}

If I do a{1}{:} will consider only the first character a

How to fix this? Thanks.

nrz
  • 10,435
  • 4
  • 39
  • 71
pac
  • 291
  • 9
  • 19

1 Answers1

2

If you only need the character vector 'aaca', you can use this:

a = {{'a'}, {'a'}, {'c'}, {'a'}};

a_CharVector = cellfun(@(x) char(x), a);

If you want the character vector 'a a c a ', you can use regexprep to add the spaces:

a_CharVectorWithSpaces = regexprep((cellfun(@(x) char(x), a)), '(.)', '$1 ');

To print a a c a with spaces and newline you can use this:

fprintf([ regexprep((cellfun(@(x) char(x), a)), '(.)', '$1 '), '\n' ]);

Edit: unnecessary anonymous function removed. @(x) is unnecessary in this case.

To get character vector 'aaca' this works:

a_CharVector = cellfun(@char, a);

And to get character vector 'a a c a ' you can use this:

a_CharVectorWithSpaces = regexprep((cellfun(@char, a)), '(.)', '$1 ');

To printf a a c a with newline:

fprintf([ regexprep((cellfun(@char, a)), '(.)', '$1 '), '\n' ]);
nrz
  • 10,435
  • 4
  • 39
  • 71
  • `@(x)` is syntax for constructing an anonymous function. However, in this case it is not necessary, as `@char` is sufficient. I edited my answer to add the solution without `@(x)` syntax. For more info about anonymous functions you can check [MATLAB documentation about anonymous functions](http://www.mathworks.com/help/techdoc/matlab_prog/f4-70115.html). Anonymous functions are quite useful with `cellfun` and `arrayfun` in some cases. – nrz Apr 14 '12 at 14:57
  • thanks. @nrz I am frankly using Fprintf to save to text file using your code it is now: fileID = fopen('a.txt', 'at'); fprintf(fileID, '%2.8s \n', cellfun(@(x) char(x), a)); fclose(fileID); – pac Apr 14 '12 at 15:21
  • So, when you use `@(x)` syntax together with `cellfun`, `x` gets all the values of the cell array one by one, so first `x` is `{'a'}`, then `x` is again `{'a'}`, then `{'c'}` ... and in this case `char` is applied to all these values `x` gets, creating a character vector of them. – nrz Apr 14 '12 at 15:25
  • You're welcome. To separate the characters I have used `regexprep`. You may want to read [MATLAB documentation about regular expressions](http://www.mathworks.com/help/techdoc/matlab_prog/f0-42649.html). `regexprep(CharVector, '(.)', '$1 ');` replaces each character of `CharVector` (expression `'(.)'` ) with its original character (`$1` in the replace string) and a space (space in the replace string) on its right side. Naturally this means that you'll get extra space in the end. To avoid this, you can replace it with another `regexprep` or using `CharVector(end) = [];` after `regexprep`. – nrz Apr 14 '12 at 16:09
  • did not work @nrz ??? Error using ==> cellfun Non-scalar in Uniform output, at index 1, output 1. Set 'UniformOutput' to false. – pac Apr 14 '12 at 17:01
  • deviations{1} ans = [0] [0.4000] [0] [0] [0] [0] [0] [0] [0] [0] – pac Apr 14 '12 at 17:04
  • can you help me as the next post write it to save to file @nrz? – pac Apr 16 '12 at 06:47