Categorical input to numerical array
이전 댓글 표시
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!
댓글 수: 9
Walter Roberson
2018년 11월 10일
xlsread or readtable should give you numeric values directly.
Elien Bellon
2018년 11월 10일
dpb
2018년 11월 10일
Walter pointed to a way to get to the specific result but both SubjectID and Name_of_runs each appear from the variable names to be the ideal candidate to be a categorical variable. Why should they be treated as numeric instead?
Elien Bellon
2018년 11월 10일
Walter Roberson
2018년 11월 10일
Can you attach a sample file? Could be made up data as long as it reproduces the issue of reading in as categorical.
Elien Bellon
2018년 11월 10일
dpb
2018년 11월 10일
, I use the num2str function..."
cv=categorical(1:3);
fn=sprintf('yourfile%03d.txt',cv(1))
fn =
'yourfile001.txt'
Or, of course, you can wrap inside double and leave num2str.
If there are really no other uses for these variables in looking for cases or other ways that are related to the variables as categories, then there may not be any strong advantage.
Elien Bellon
2018년 11월 10일
dpb
2018년 11월 11일
Well, actually, it's going to char() that is needed to use str2num, not double().
But, the problem is more fundamental in this case owing to the structure that the variable is a composite one, not the single value until you convert it as either Walter or I showed...
답변 (2개)
Walter Roberson
2018년 11월 10일
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.
댓글 수: 2
Elien Bellon
2018년 11월 10일
Walter Roberson
2018년 11월 10일
편집: Walter Roberson
2018년 11월 10일
In the above code, MATLAB already knows it. It already knows that, for example, Name_of_runs{2} is a numeric row vector of length 4, and that Name_of_runs{17} is a numeric row vector of length 3.
dpb
2018년 11월 10일
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.
카테고리
도움말 센터 및 File Exchange에서 Spreadsheets에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!