Asked by Elien Bellon
on 10 Nov 2018

Hi,

I have imported data from an excel file with 2 columns with numerical info using the import wizard. In the excel: The first column contains the subject numbers (e.g., 1, 2, 3, 4, ...). The second column contains different digits per cel that refer to the name of the task the subject did (e.g., [3 4 8 9] for subject 1, [4 5 6 8 9] for subject 2)

My goal is to have 2 numeric variables in Matlab, i.e., "SubjectID" and "Name_of_runs" So: I want both columns to be seperate in my workspace and I want them both to be numeric.

Using the import wizard, the first column is created as I want, I can create a numeric variable.

However, the second column with the names of the tasks is not numeric, but categorical input. I want it to be numerical. For example, I get [3 4 8 9] as my ans for subject 1, a 1x1 categorical array. I want a 1x4 numerical array.

How can I solve this?

Thanks in advance!

Answer by Walter Roberson
on 10 Nov 2018

t = readtable('names_SubjectID_Runs.xlsx');

SubjectID = t.SubjectID;

Name_of_runs = cellfun(@str2double,regexp(t.name_of_runs,'\d+', 'match'),'uniform', 0);

Name_of_runs cannot be numeric because you have a different number of runs for different rows. Instead it is a cell array of numeric vectors.

Elien Bellon
on 10 Nov 2018

Walter Roberson
on 10 Nov 2018

Sign in to comment.

Answer by dpb
on 10 Nov 2018

The problem is your spreadsheet is structured such that the runs variable array is stored as a text string of a series of values enclosed in braces, each array in a single cell instead of as numbers; one per cell. Matlab did best it knew how to retrieve it.

t=readtable('names_SubjectID_Runs.xlsx');

>> cellfun(@str2num,t.name_of_runs,'uni',0)

ans =

26×1 cell array

{1×4 double}

{1×4 double}

...

{1×4 double}

{1×3 double}

{1×3 double}

{1×3 double}

{1×3 double}

>>

Unfortunately, there aren't the same number of observations in each row so you'll have to either augment the shorter w/ NaN or use a cell array to hold the values.

But, the above shows how to convert what you have; if could change the way the data are saved into Excel could solve the problem there if wanted to, instead, altho this is simple-enough once know what the issue actually is.

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 9 Comments

## Walter Roberson (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/429096-categorical-input-to-numerical-array#comment_635333

## Elien Bellon (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/429096-categorical-input-to-numerical-array#comment_635335

## dpb (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/429096-categorical-input-to-numerical-array#comment_635338

## Elien Bellon (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/429096-categorical-input-to-numerical-array#comment_635340

## Walter Roberson (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/429096-categorical-input-to-numerical-array#comment_635350

## Elien Bellon (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/429096-categorical-input-to-numerical-array#comment_635357

## dpb (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/429096-categorical-input-to-numerical-array#comment_635370

## Elien Bellon (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/429096-categorical-input-to-numerical-array#comment_635421

## dpb (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/429096-categorical-input-to-numerical-array#comment_635718

Sign in to comment.