Arrays of strings

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

Arrays of strings

Ted.Harding
Hi All,

Am I missing something?

I want to set up an array of strings (actually a list of filenames which I
want octave to work through, to be given one by one as string arguments to
a function).

In Matlab this is straightforward, if bulky: a string can be stored as a
vector of floats, one for the ASCII code of each character, and a matrix
can be constructed of which each row is the sequence of numbers for the
ASCII characters of a string (you would have to pad some strings with
spaces to make them all the same length).

In Matlab you could write

    names = ['file1'; 'file2'];

or

    names = ['file1'
             'file2' ];

and get the i-th names as names(i,:).

I can not find any way of doing such a thing in octave. Construction as
above generates an error. The function "abs" in Matlab returns the ASCII
numerical codes: abs('file1') = [102 105 108 101 49]. This also generates
an error in octave (otherwise could be used to make a numerical matrix).

Although octave has conversions going the other way (num2str, int2str),
there seem to be no way to set up an array of strings, or even to get at
individual elements of a string.

Can anyone help?

Ted.                                    ([hidden email])

Reply | Threaded
Open this post in threaded view
|

Re: Arrays of strings

niles-3



jwe writes:

>It's not pretty, but you can do this:
>
>  s = toascii (s); s = setstr (s (INDEX));

Yeah I hadn't thought of this, actually very useful!
Here's what I did I wrote a tiny function stostr:

function var = stostr(var, loc, string)
var(loc,1:max(size(toascii(string)))) = toascii(string);

Now, in an m-file you can to things like..

states = 0;
k=1;
states = stostr(states,k++,"Beta, Side Silp Angle (Degrees)");
states = stostr(states,k++,"r, Yaw Rate (Degrees/sec)");
states = stostr(states,k++,"p, Roll Rate (Degrees/sec)");
states = stostr(states,k++,"Phi, Roll Angle (Degrees)");
states = stostr(states,k++,"Psi, Yaw Angle (Degrees)");
states = stostr(states,k++,"w, Wind Angle (Degrees)");

--------------------------
then later:

for k = 1:6       % States
  ylabel(setstr(states(k,:)))
  plot(t,x(:,k))
end
----------------------

        thought others might find this useful...

        Thanks for the idea!
        Rick.

Reply | Threaded
Open this post in threaded view
|

Re: Arrays of strings

John W. Eaton-6
[hidden email] <[hidden email]> showed how
to simulate vectors of strings using numeric matrices and
toascii/setstr:

: Here's what I did I wrote a tiny function stostr:
:
: function var = stostr(var, loc, string)
: var(loc,1:max(size(toascii(string)))) = toascii(string);

Yes, this is about the best you can do with 1.1.1.  For 1.2, Octave
will support vectors of strings.  Indexing will work as will
conversion to numeric matrices using toascii (or abs, if
implicit_str_to_num_ok is set is set to true).

Also, the internal representation will be 1-byte characters, not be a
matrix of doubles, so strings should be more space-efficient in Octave
compared to Matlab, and Octave will not force to to pad strings to
the same length in order to put them in an array.  For example,

  foo = ["this"; "is"; "an"; "array"; "of"; "strings"]

will be perfectly acceptable.

Thanks,

jwe