1

In Octave:

Given a vector

a = 1:3

How to turn vector "a" into a cell array of strings

{'1','2','3'}

(Output of this:)

ans =
{
  [1,1] = 1
  [1,2] = 2
  [1,3] = 3
}

(question end)


Further information: my aim is to use a as the input of strcat('x',a) to get

ans =
{
  [1,1] = x1
  [1,2] = x2
  [1,3] = x3
}

Other solutions how to reach this aim are welcome, but the main question is about getting an array of strings directly from a vector.

This question is a follow-up from cell array, add suffix to every string which could not help me, since strcat('x',{'1','2','3'}) works, but strcat('x', num2cell(1:3)) does not:

>> strcat('x', num2cell(1:3))
warning: implicit conversion from numeric to char
warning: called from
    strcat at line 121 column 8
    C:/Users/USERNAME/AppData/Local/Temp/octave/octave_ENarag.m at line 1 column 1

warning: implicit conversion from numeric to char
warning: called from
    strcat at line 121 column 8
    C:/Users/USERNAME/AppData/Local/Temp/octave/octave_ENarag.m at line 1 column 1

warning: implicit conversion from numeric to char
warning: called from
    strcat at line 121 column 8
    C:/Users/USERNAME/AppData/Local/Temp/octave/octave_ENarag.m at line 1 column 1

ans =
{
  [1,1] = x[special sign "square"]
  [1,2] = x[special sign "square"]
  [1,3] = x[special sign "square"]
}

Also, the Matlab function string(a) according to Matlab: How to convert cell array to string array?, is not yet implemented:

"The 'string' function is not yet implemented in Octave.`

questionto42
  • 7,175
  • 4
  • 57
  • 90

2 Answers2

1

You can combine cellstr and int2str:

strcat('x', cellstr(int2str((1:3).')))
or
cellstr(strcat('x', int2str((1:3).')))

You can also combine sprintf and ostrsplit:

ostrsplit(sprintf('x%d ', 1:3), ' ', true)
or
strcat('x', ostrsplit(sprintf('%d ', 1:3), ' ', true))

You can also use num2str instead of int2str.

rahnema1
  • 15,264
  • 3
  • 15
  • 27
  • The first two give me special sign (looks like a [square]) after the x `>> strcat('x', cellstr(int2str((1:3).'))) ans = { [1,1] = x [square] }` and `>> cellstr(strcat('x', int2str((1:3).'))) ans = { [1,1] = x [square] }`. The ostrsplit solution works, but I would like to know the solution using strcat, and therefore the question is not solved: how to get `>> {'1','2','3'} ans = { [1,1] = 1 [1,2] = 2 [1,3] = 3 }` from "a". Thus the question 'How to turn vector "a" into a cell vector of strings'. – questionto42 Jan 04 '21 at 05:46
  • no, same thing, just a `[1,1] = [square]` sign as output – questionto42 Jan 04 '21 at 06:02
  • @questionto42 They work for me. Does `a = 1:3;cellstr(int2str(a(:)))` work on your system? – rahnema1 Jan 04 '21 at 06:03
  • Try `a = 1:3;cellstr(num2str(a(:)))` – rahnema1 Jan 04 '21 at 06:05
  • if you also transpose it. Then you get `>> cellstr(num2str(a(:)))'` output `ans = { [1,1] = 1 [1,2] = 2 [1,3] = 3 }`. I do not know why the other solutions give me a special sign [square], though. I use Octave 6.1.0 – questionto42 Jan 04 '21 at 06:09
  • They also work on an [online](https://octave-online.net/) version of Octave. – rahnema1 Jan 04 '21 at 06:11
  • @questionto42 I edited my answer combining `ostrsplit` and `strcat`. – rahnema1 Jan 04 '21 at 07:02
  • Could you also add the workaround `cellstr(num2str(a(:)))'` perhaps? Even if we can expect that [Octave (GUI): wrong output and with a special sign “\[square\]” while online Octave works](https://stackoverflow.com/questions/65558467/octave-gui-wrong-output-and-with-a-special-sign-square-while-online-octav) will be solved in future, that workaround is also interesting. – questionto42 Jan 04 '21 at 07:07
  • @questionto42 I think `int2str` is sufficient. If you want you can post it as a separate answer! – rahnema1 Jan 04 '21 at 07:10
  • Thanks, no, I will not post your idea as a separate answer. Then the comments here should suffice. Think it over, it is just one more way, and you give four ways anyway. Not important, though, you decide. It is just that I cannot use the first two solutions at the moment for these technical reasons, and perhaps I am not the only one. – questionto42 Jan 04 '21 at 07:13
  • 1
    @questionto42 `num2str` is also added! – rahnema1 Jan 04 '21 at 07:16
  • Reason for the trouble with int2str is clear now: I had accidentally shadowed the int2str() function with `int2str = [1:3]`. Beginner's mistake. Still, the question remains valid. – questionto42 Jan 04 '21 at 08:43
1

Just to add the other 'obvious' solution

arrayfun( @(x) strcat('x', num2str(x)), a, 'UniformOutput', false )

or equivalently

arrayfun( @(x) sprintf('x%d', x), a, 'UniformOutput', false )
Tasos Papastylianou
  • 21,371
  • 2
  • 28
  • 57