Flying pig
Flying pig

Reputation: 612

How can I import string from Excel into MATLAB?

I want to import a row of headers form Excel into matlab then put each header into its according variable's name in MATLAB. Just like the picture below shows, I import all the variables into a matrix 'X', then I would like to import the header one by one to rename all the columns. Is there any way to do this? enter image description here

Upvotes: 1

Views: 14658

Answers (2)

Gunther Struyf
Gunther Struyf

Reputation: 11168

See the documentation of xlsread; you have to load all the data:

from matlab doc: Example

Request the numeric data, text, and a copy of the unprocessed (raw) data from myExample.xlsx:

[ndata, text, alldata] = xlsread('myExample.xlsx')

ndata =
     1     2     3
     4     5   NaN
     7     8     9

text = 
    'First'    'Second'    'Third'
    ''         ''          ''     
    ''         ''          'x'    

alldata = 
    'First'    'Second'    'Third'
    [    1]    [     2]    [    3]
    [    4]    [     5]    'x'    
    [    7]    [     8]    [    9]

In case you know in which cells the headers are, you can also specify a range to read from the excel file:

[~, headers, ~ ] = xlsread('myExample.xlsx','A1:C1');

this gives (for the example data used above ^^):

headers = 
    'First'    'Second'    'Third'

Upvotes: 6

Superbest
Superbest

Reputation: 26622

You can import Excel files using the workspace GUI or help xlsread.

You can also copy and paste data by first creating an empty cell array with a = cell(1) from the command prompt, then double clicking the variable and copy/pasting from Excel into it (use "Paste Excel data").

Upvotes: 0

Related Questions