-
Notifications
You must be signed in to change notification settings - Fork 3
/
cell2str.m
40 lines (37 loc) · 1.29 KB
/
cell2str.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
function string = cell2str(cellstr)
%CELL2STR Convert a 2-D cell array of strings to a string in MATLAB syntax.
% STR = CELL2STR(CELLSTR) converts the 2-D cell-string CELLSTR to a
% MATLAB string so that EVAL(STR) produces the original cell-string.
% Works as corresponding MAT2STR but for cell array of strings instead of
% scalar matrices.
%
% Example
% cellstr = {'U-234','Th-230'};
% cell2str(cellstr) produces the string '{''U-234'',''Th-230'';}'.
%
% See also MAT2STR, STRREP, CELLFUN, EVAL.
% Developed by Per-Anders Ekstrm, 2003-2007 Facilia AB.
if nargin~=1
error('CELL2STR:Nargin','Takes 1 input argument.');
end
if ischar(cellstr)
string = ['''' strrep(cellstr,'''','''''') ''''];
return
end
if ~iscellstr(cellstr)
error('CELL2STR:Class','Input argument must be cell array of strings.');
end
if ndims(cellstr)>2
error('CELL2STR:TwoDInput','Input cell array must be 2-D.');
end
ncols = size(cellstr,2);
for i=1:ncols-1
cellstr(:,i) = cellfun(@(x)['''' strrep(x,'''','''''') ''','],...
cellstr(:,i),'UniformOutput',false);
end
if ncols>0
cellstr(:,ncols) = cellfun(@(x)['''' strrep(x,'''','''''') ''';'],...
cellstr(:,ncols),'UniformOutput',false);
end
cellstr = cellstr';
string = ['{' cellstr{:} '}'];