# Straightforward Copy & Paste

Sean's pick this week is Straightforward Copy & Paste by Yvan Lengwiler.

When presenting seminars on MATLAB, I often ask customers how to get data into and out of Excel. The answers are typically commands such as xlsread and xlswrite. These are great, and are often the best way to do this for an application. However, they can be overkill if you just need to move a single array to your active Excel sheet every now and then.

Let's see what Yvan has done:

% A simple magic square:
A = magic(5)
A =
17    24     1     8    15
23     5     7    14    16
4     6    13    20    22
10    12    19    21     3
11    18    25     2     9


Because I left the semicolon off, it was echoed to the command line.

I'll copy it from there and paste it into Excel:

It pasted it in as a string into cells A1:A5. That's not what I wanted!

copy(A)

Now in Excel, when I paste, it'll paste the way I intended:

We could get that same functionality with just MATLAB by interactively opening the variable in the Variable Editor (or by using openvar) and then copying from there.

So where does this really shine? What if we want to extract the middle part of some text or a table from a website and paste it into MATLAB?

Since Yvan's copy and paste allow linefeeds and separators, it's easy to bring in complicated data like this.

Let's get a list of all of MathWorks' products from the website into MATLAB. First, highlight and copy the table for all products:

% Pause so I can go copy it, then press a key when ready
pause

% Use Yvan's Paste and show first few results
products = paste();
disp(products(1:10))
    [1x0 char]
[1x0 char]
'MATLAB® Product Family'
[1x0 char]
'MATLAB '
[1x0 char]
'Parallel Computing'
'Parallel Computing Toolbox '
'MATLAB Distributed Computing Server '
[1x0 char]


This gives us the whole table including headers and empty elements. We want only the products. The product strings have a space at the end of them that we can use to determine if the non-empty cells are products.

% Define an anonymous function to test if each cell is a product
% I.e. non-empty and the last element a space
isproduct = @(x)~isempty(x) && isspace(x(end));

% Keep only the cells which are products
products = products(cellfun(isproduct,products));

% Deblank it to remove those precious spaces at the end.  Keep only
% unique products since some show up a few times under different categories.
products = unique(deblank(products));
disp(products)
    'Aerospace Blockset'
'Aerospace Toolbox'
'Bioinformatics Toolbox'
'Communications System Toolbox'
'Computer Vision System Toolbox'
'Control System Toolbox'
'Curve Fitting Toolbox'
'DO Qualification Kit (for DO-178)'
'DSP System Toolbox'
'Data Acquisition Toolbox'
'Database Toolbox'
'Datafeed Toolbox'
'Econometrics Toolbox'
'Embedded Coder'
'Filter Design HDL Coder'
'Financial Instruments Toolbox'
'Financial Toolbox'
'Fixed-Point Designer'
'Fuzzy Logic Toolbox'
'Gauges Blockset'
'Global Optimization Toolbox'
'HDL Coder'
'HDL Verifier'
'IEC Certification Kit (for ISO 26262 and IEC 61508)'
'Image Acquisition Toolbox'
'Image Processing Toolbox'
'Instrument Control Toolbox'
'LTE System Toolbox'
'MATLAB'
'MATLAB Builder EX (for Microsoft Excel)'
'MATLAB Builder JA (for Java language)'
'MATLAB Builder NE (for Microsoft .NET Framework)'
'MATLAB Coder'
'MATLAB Compiler'
'MATLAB Distributed Computing Server'
'MATLAB Production Server'
'MATLAB Report Generator'
'Mapping Toolbox'
'Model Predictive Control Toolbox'
'Model-Based Calibration Toolbox'
'Neural Network Toolbox'
'OPC Toolbox'
'Optimization Toolbox'
'Parallel Computing Toolbox'
'Partial Differential Equation Toolbox'
'Phased Array System Toolbox'
'Polyspace Bug Finder'
'Polyspace Code Prover'
'RF Toolbox'
'Real-Time Windows Target'
'Robust Control Toolbox'
'Signal Processing Toolbox'
'SimBiology'
'SimDriveline'
'SimElectronics'
'SimEvents'
'SimHydraulics'
'SimMechanics'
'SimPowerSystems'
'SimRF'
'Simscape'
'Stateflow'
'Statistics Toolbox'
'Symbolic Math Toolbox'
'System Identification Toolbox'
'SystemTest'
'Vehicle Network Toolbox'
'Wavelet Toolbox'


And there we have it, MathWorks' 82 products!

How many of those products have you heard of (or not heard of)? I'm proud to say that I've only not heard of two of them; I'll send some MathWorks swag to the first person to guess each correctly :) Limit: three guesses per day

Do you have complicated data that you need to get into MATLAB? If so, what are your favorite tools or workflows to do this?

Give this a try and let us know what you think here or leave a comment for Yvan.

Published with MATLAB® R2013b

|