Combining cell arrays and copying to clipboard

10 views (last 30 days)
I cannot get two cell arrays to catenate and then copy to clipboard.
%Get two cell arrays
s=get(handles.uitable1,'data');
cname=get(handles.uitable1, 'columnname')
*%there are 3 columns, first is text, other two are floats)*
ab = cat(1, cname', (s));
str=sprintf('%s = %s\t%f\t%f\n', ab{:})
clipboard ( 'copy', str );
This is the output of ab:
'title1' 'title2' 'title3'
'sample1' '0.9940' [ 0.2370]
'sample2' '0.9948' [ 0.2320]
'sample3' '0.9950' [ 0.2211]
  3 Comments
Jason
Jason on 23 Jan 2015
Hi. Im sorry for not being clear. I have concatenated column titles from a uitable to the data. My aim is to be able to copy to clipboard, so I can paste in other documents.
Once i have concatenated, the output i want to copy to clipboard is exactly like
'title1' 'title2' 'title3'
'sample1' '0.9940' [ 0.2370]
'sample2' '0.9948' [ 0.2320]
'sample3' '0.9950' [ 0.2211]
I don't care what format its copied as into clipboard.
Jason
Jason on 23 Jan 2015
just to add: I have found out that this almost works:
str = [];
newline = sprintf('\n');
for i = 1:size(ab,1);
row = sprintf('%s\t', ab{i,:});
row(end) = newline;
str = [str row]; %#ok<AGROW>
end
str
clipboard('copy',str);
The only undesired effect is that the last column is in exponent form which I don't want (This is what is pasted into excel)
title1 title2 title3
sample1 0.994 2.37E-01
sample2 0.9948 2.32E-01
sample3 0.995 2.21E-01

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 23 Jan 2015
This works:
ab = ab'; %transpose so the values arrive in the right order for the 2nd sprintf
str = sprintf('%s\t%s\t%s\n', ab{:, 1}); %print first row exclusively as text
str = [str sprintf('%s\t%s\t%f\n', ab{:, 2:end})]
  3 Comments
Guillaume
Guillaume on 23 Jan 2015
The cropping of 0.990 to 0.99 is down to excel, there's nothing you can do in matlab about that. Excel automatically convert the text to numbers and by default does not display trailing zeros.
You can modify the cell formating in excel to always have 3 decimals. That will also take care of 0.9948.
You could round the 0.9448 in matlab, but first you'd have to transform your column of numbers as strings into proper numbers
%before transposing ab:
ab(2:end, 2) = cellfun(@str2num, ab(2:end, 2), 'UniformOutput', false)
You can then use the formatting options of sprintf:
ab = ab'; %transpose so the values arrive in the right order for the 2nd sprintf
str = sprintf('%s\t%s\t%s\n', ab{:, 1}); %print first row exclusively as text
str = [str sprintf('%s\t%1.3f\t%1.3f\n', ab{:, 2:end})]

Sign in to comment.

More Answers (0)

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!