Convert a vector/array/matrix of values into a comma-separated string

版本 1.0.0.0 (2.2 KB) 作者: Yair Altman
Converts input of unknown size/type into compact single-line string suitable for log files & uitable
2.7K 次下载
更新时间 2009/2/9

无许可证

Vec2str converts an input of unknown size/type into a compact single-line comma-delimited string which is suitable for log files and uitable cells. Accepts any dimension & combination of numbers, logicals, cells, Java/COM handles etc.

Syntax:
[str, errMsg] = vec2str(vec, format, numericFlag, encloseFlag)

Inputs:
- vec - vector/array/matrix of values (mandatory)
- format - optional format/precision argument accepted by the num2str() function
- numericFlag - optional flag indicating whether to keep scalar (non-vectors) numeric value as-is (default = 0 or false)
- encloseFlag - optional flag indicating whether to enclose numeric vectors with [], cells with {} (default = 1 or true)

Outputs:
- str - string representation of vec (or numeric value if numericFlag was requested)
- errMsg - contains a non-empty string if an error occured during processing

Examples:
vec2str('abcd') => 'abcd' (string input always returns as-is)
vec2str(pi) => '3.1416'
vec2str(pi,'=> %f <=') => '=> 3.141593 <=' (see SPRINTF for format explanation)
vec2str(pi,8) => '3.1415927' (8 significant digits)
vec2str(pi,8,0) => '3.1415927' (default numericFlag=0 so same result as above)
vec2str(pi,8,1) => 3.14159265358979 (keeps numeric type, overrides format request)
vec2str(pi,[],[],1) => '3.1416' (scalar values are NEVER enclosed with [])
vec2str(magic(3)) => '[8,1,6; 3,5,7; 4,9,2]'
vec2str(magic(3) > 4) => '[true,false,true; false,true,true; false,true,false]'
vec2str(1:4) => '[1,2,3,4]'
vec2str(1:4,[],[],1) => '[1,2,3,4]' (default encloseFlag=1 so same result as above)
vec2str(1:4,[],[],0) => '1,2,3,4'
vec2str({3,2:4,'ryt',NaN, -inf,sqrt(-i),{2,1},java.lang.String('ert')})
=> '{3,[1x3 double],'ryt',NaN,-Inf,[0.707106781186548-0.707106781186548i],{1x2 cell},[1x1 java.lang.String]}'
vec2str({3,2:4,'ryt',NaN, -inf,sqrt(-i),{2,1},java.lang.String('ert')},3,'',0)
=> ' '3','2 3 4','ryt','NaN','-Inf','0.707-0.707i',{1x2 cell},[1x1 java.lang.String]'

See also: num2str, int2str, sprintf

引用格式

Yair Altman (2024). Convert a vector/array/matrix of values into a comma-separated string (https://www.mathworks.com/matlabcentral/fileexchange/22937-convert-a-vector-array-matrix-of-values-into-a-comma-separated-string), MATLAB Central File Exchange. 检索时间: .

MATLAB 版本兼容性
创建方式 R2008b
兼容任何版本
平台兼容性
Windows macOS Linux
类别
Help CenterMATLAB Answers 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
版本 已发布 发行说明
1.0.0.0