Combining cell arrays and copying to clipboard
10 views (last 30 days)
Show older comments
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
Accepted Answer
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
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})]
More Answers (0)
See Also
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!