Code covered by the BSD License  

Highlights from
vector to string

5.0

5.0 | 1 rating Rate this file 3 Downloads (last 30 days) File Size: 2.26 KB File ID: #26453

vector to string

by Sven Bossuyt

 

22 Jan 2010 (Updated 01 Feb 2010)

Converts a vector to a string, using the colon operator to represent linearly spaced subvectors.

| Watch this File

File Information
Description

This is a small M-file that can be thought of as the inverse of the colon operator: it takes a linearly spaced row vector and generates a compact notation for that vector.
It handles concatenations of such vectors too, and will in fact for any real array (subject to considerations of numerical precision) generate a valid string representation of that array as a reshaped row vector.
I wrote this because I found myself using input dialog boxes in a GUI, where a numeric vector is entered as a string to be parsed by str2num, and I wanted to provide the current value of the vector as the default answer. mat2str already does this, but the result is unwieldy for all but the smallest arrays, so that was not really useful in this context.

MATLAB release MATLAB 7.9 (2009b)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (5)
22 Jan 2010 Rob Campbell

Although I personally don't have any use for this, it is rather nice!

24 Jan 2010 Jan Simon

See also vect2colon:
http://www.mathworks.com/matlabcentral/fileexchange/22076

25 Jan 2010 Sven Bossuyt

Somehow I didn't come across vect2colon when I looked for a way to do this. Out of curiosity I compared both implementations, and they're not quite interchangeable.
For instance
>> vect2colon([1 3 5 5 9 8 7 6 5])
ans =
[ 1:2:5 9:6]
>> eval(ans)
ans =
     1 3 5
whereas
>> vec2str([1 3 5 5 9 8 7 6 5])
ans =
[1:2:5,5,9:-1:5]
>> eval(ans)
ans =
     1 3 5 5 9 8 7 6 5
Perhaps I am missing something, perhaps it is a bug in vect2colon, but the result of vec2str makes more sense to me. I don't actually use this with repeated values, but I don't see why they should be 'not allowed'.

Anyway... thanks for pointing out that there already was a similar submission.

26 Jan 2010 Jan Simon

H1-line, help section, examples, author and history (helps to search for updates in the FEX later), some comments in the source, "See also:".
This function handles non-trivial cases also: ([1 4 8;2 5 6;3 10 4]) => 'reshape([ 1:5, 10:-2:4 ],[3 3])'
BTW: This is not *vector*to-string anymore.
The author cares for usability, fixes bugs and cares about comments. Thanks!

26 Jan 2010 Jan Simon  
Please login to add a comment or rating.
Updates
25 Jan 2010

* fixed bug where end of one subsequence is start of next one
* added spaces between subsequences, to improve readability
* changed behaviour for subsequences of length 2

01 Feb 2010

generate error immediately if array is not numeric
changed logic of algorithm, to also handle NaN elements

Tag Activity for this File
Tag Applied By Date/Time
gui Sven Bossuyt 22 Jan 2010 10:56:44
input dialog Sven Bossuyt 22 Jan 2010 10:56:44
gui Bhavik 06 Feb 2010 04:55:38

Contact us at files@mathworks.com