-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmat2clip.m
149 lines (127 loc) · 3.89 KB
/
mat2clip.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
function out = mat2clip(a, delim)
%MAT2CLIP Copies matrix to system clipboard.
%
% MAT2CLIP(A) copies the contents of 2-D matrix A to the system clipboard.
% A can be a numeric array (floats, integers, logicals), character array,
% or a cell array. The cell array can have mixture of data types.
%
% Each element of the matrix will be separated by tabs, and each row will
% be separated by a NEWLINE character. For numeric elements, it tries to
% preserve the current FORMAT. The copied matrix can be pasted into
% spreadsheets.
%
% OUT = MAT2CLIP(A) returns the actual string that was copied to the
% clipboard.
%
% MAT2CLIP(A, DELIM) uses DELIM as the delimiter between columns. The
% default is tab (\t).
%
% Example:
% format long g
% a = {'hello', 123;pi, 'bye'}
% mat2clip(a);
% % paste into a spreadsheet
%
% format short
% data = {
% 'YPL-320', 'Male', 38, true, uint8(176);
% 'GLI-532', 'Male', 43, false, uint8(163);
% 'PNI-258', 'Female', 38, true, uint8(131);
% 'MIJ-579', 'Female', 40, false, uint8(133) }
% mat2clip(data);
% % paste into a spreadsheet
%
% mat2clip(data, '|'); % using | as delimiter
%
% See also CLIPBOARD.
% VERSIONS:
% v1.0 - First version
% v1.1 - Now works with all numeric data types. Added option to specify
% delimiter character.
%
% Copyright 2009 The MathWorks, Inc.
%
% Inspired by NUM2CLIP by Grigor Browning (File ID: 8472) Matlab FEX.
error(nargchk(1, 2, nargin, 'struct'));
if ndims(a) ~= 2
error('mat2clip:Only2D', 'Only 2-D matrices are allowed.');
end
% each element is separated by tabs and each row is separated by a NEWLINE
% character.
sep = {'\t', '\n', ''};
if nargin == 2
if ischar(delim)
sep{1} = delim;
else
error('mat2clip:CharacterDelimiter', ...
'Only character array for delimiters');
end
end
% try to determine the format of the numeric elements.
switch get(0, 'Format')
case 'short'
fmt = {'%s', '%0.5f' , '%d'};
case 'shortE'
fmt = {'%s', '%0.5e' , '%d'};
case 'shortG'
fmt = {'%s', '%0.5g' , '%d'};
case 'long'
fmt = {'%s', '%0.15f', '%d'};
case 'longE'
fmt = {'%s', '%0.15e', '%d'};
case 'longG'
fmt = {'%s', '%0.15g', '%d'};
otherwise
fmt = {'%s', '%0.5f' , '%d'};
end
if iscell(a) % cell array
a = a';
floattypes = cellfun(@isfloat, a);
inttypes = cellfun(@isinteger, a);
logicaltypes = cellfun(@islogical, a);
strtypes = cellfun(@ischar, a);
classType = zeros(size(a));
classType(strtypes) = 1;
classType(floattypes) = 2;
classType(inttypes) = 3;
classType(logicaltypes) = 3;
if any(~classType(:))
error('mat2clip:InvalidDataTypeInCell', ...
['Invalid data type in the cell array. ', ...
'Only strings and numeric data types are allowed.']);
end
sepType = ones(size(a));
sepType(end, :) = 2; sepType(end) = 3;
tmp = [fmt(classType(:));sep(sepType(:))];
b=sprintf(sprintf('%s%s', tmp{:}), a{:});
elseif isfloat(a) % floating point number
a = a';
classType = repmat(2, size(a));
sepType = ones(size(a));
sepType(end, :) = 2; sepType(end) = 3;
tmp = [fmt(classType(:));sep(sepType(:))];
b=sprintf(sprintf('%s%s', tmp{:}), a(:));
elseif isinteger(a) || islogical(a) % integer types and logical
a = a';
classType = repmat(3, size(a));
sepType = ones(size(a));
sepType(end, :) = 2; sepType(end) = 3;
tmp = [fmt(classType(:));sep(sepType(:))];
b=sprintf(sprintf('%s%s', tmp{:}), a(:));
elseif ischar(a) % character array
% if multiple rows, convert to a single line with line breaks
if size(a, 1) > 1
b = cellstr(a);
b = [sprintf('%s\n', b{1:end-1}), b{end}];
else
b = a;
end
else
error('mat2clip:InvalidDataType', ...
['Invalid data type. ', ...
'Only cells, strings, and numeric data types are allowed.']);
end
clipboard('copy', b);
if nargout
out = b;
end