convert cell in string

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

convert cell in string

turbofib
hi,
i want to convert cell in string

Example :

 typeinfo(g)

ans = cell


how can i do to convert?





--
Sent from: http://octave.1599824.n4.nabble.com/Octave-Maintainers-f1638794.html

Reply | Threaded
Open this post in threaded view
|

Re: convert cell in string

Daniel Sebald
On 07/13/2018 06:48 PM, turbofib wrote:

> hi,
> i want to convert cell in string
>
> Example :
>
>   typeinfo(g)
>
> ans = cell
>
>
> how can i do to convert?

I sent an email to turbofib about options.

In experimenting I came across a couple things.

1) I wonder if some structure element names should be disallowed because
they can't be accessed directly; only via the getfield() function.  Some
examples:

octave:62> g = {"A","B","C"};
octave:63> s = cell2struct(g, ".");
octave:64> s..
parse error:

   syntax error

 >>> s..
       ^

octave:64> getfield (s(2), ".")
ans = B
octave:65> s = cell2struct(g, " ");
octave:66> s.
parse error:

   syntax error

 >>> s.
       ^

octave:66> s = cell2struct(g, "9");
octave:67> s.9
parse error:

   syntax error

 >>> s.9
       ^

It's fine I suppose, but just strange.


2) Should the following be allowed?

octave:69> [~ s1 s2] = g{:}
parse error:

   invalid left hand side of assignment

 >>> [~ s1 s2] = g{:}

The tilde can be at the end of the argument list without error, but
nowhere else.

Dan

Reply | Threaded
Open this post in threaded view
|

Re: convert cell in string

turbofib
hi daniel


the reason why I want to convert the data type is to use substr()


if i've a type of "cell " data i see this error  :

error: substr: S must be a string or string array

For this reason I'm trying to convert from cell to string

 



--
Sent from: http://octave.1599824.n4.nabble.com/Octave-Maintainers-f1638794.html

Reply | Threaded
Open this post in threaded view
|

Re: convert cell in string

PhilipNienhuis
In reply to this post by turbofib
turbofib wrote

> hi,
> i want to convert cell in string
>
> Example :
>
>  typeinfo(g)
>
> ans = cell
>
> how can i do to convert?

If g is scalar (check with "size (g)"), s/th like
 g = g{1}
-or-
cell2mat (g)

Philip



--
Sent from: http://octave.1599824.n4.nabble.com/Octave-Maintainers-f1638794.html

Reply | Threaded
Open this post in threaded view
|

Re: convert cell in string

Andreas Weber-6
In reply to this post by turbofib
Am 14.07.2018 um 10:39 schrieb turbofib:
> the reason why I want to convert the data type is to use substr()

Ah, a typical XY problem.

You are asking how to "convert a cell to string" but
what you really want to do is to run "substr" on a cell string.

The solution for this is to use "cellfun":

octave:> cellfun (@(x) substr (x, 1, 2), {"foo", "hello", "world"},
"UniformOutput", false)
ans =
{
  [1,1] = fo
  [1,2] = he
  [1,3] = wo
}

-- Andy

Reply | Threaded
Open this post in threaded view
|

Re: convert cell in string

Ian McCallion
Dear Turbofib,

Your question has several meanings. Suppose cell array is:

  {97, 98}

Please say what you want the result to be:

  {'97', '98'}

  {'a','b'}

   "97 98"

Or give us an example of what you want.

Then we can give you the code to achieve it.

Ian



On 19 July 2018 at 21:32, turbofib <[hidden email]> wrote:
in cell array i have a scalar not string

i've a scalar and i want get string





--
Sent from: http://octave.1599824.n4.nabble.com/Octave-Maintainers-f1638794.html


Reply | Threaded
Open this post in threaded view
|

Re: convert cell in string

Ian McCallion
    a = {97 98}
    s = cellfun(@dec2base, a, {10}, 'UniformOutput', false)

Please remember to use "reply all" for replies in future.

Ian

On 20 July 2018 at 12:36, <[hidden email]> wrote:

i want this:


{'97', '98'}


thanks

Il 20 luglio 2018 alle 12.28 Ian McCallion <[hidden email]> ha scritto:

Dear Turbofib,

Your question has several meanings. Suppose cell array is:

  {97, 98}

Please say what you want the result to be:

  {'97', '98'}

  {'a','b'}

   "97 98"

Or give us an example of what you want.

Then we can give you the code to achieve it.

Ian



On 19 July 2018 at 21:32, turbofib <[hidden email]> wrote:
in cell array i have a scalar not string

i've a scalar and i want get string





--
Sent from: http://octave.1599824.n4.nabble.com/Octave-Maintainers-f1638794.html



Reply | Threaded
Open this post in threaded view
|

Re: convert cell in string

Rik-4
In reply to this post by turbofib
On 07/20/2018 09:00 AM, [hidden email] wrote:
Subject:
Re: convert cell in string
From:
Ian McCallion [hidden email]
Date:
07/20/2018 03:28 AM
To:
turbofib [hidden email]
CC:
[hidden email]
List-Post:
[hidden email]
Precedence:
list
MIME-Version:
1.0
References:
[hidden email] [hidden email] [hidden email] [hidden email] [hidden email]
In-Reply-To:
[hidden email]
Message-ID:
[hidden email]
Content-Type:
multipart/alternative; boundary="000000000000efec9f05716bc1c1"
Message:
5

Dear Turbofib,

This question should be discussed on the octave-help mailing list rather than the octave-maintainers list.  Please change your CC field to [hidden email] for future e-mails.

--Rik