How to store the column names for each numerical values for respective rows in an array
9 views (last 30 days)
Show older comments
I'd like to get from a raw matrix like this:
several cell arrays of strings like this:
One = 'A' 'C' 'D' 'F'
Two = 'B' 'G'
How could I do this?
2 Comments
Jan
on 6 Jun 2018
What are your input data? The screen shot is not clear. Prefer to post code, which produces the input.
What is the relation between the array and the output? Why is
One = 'A' 'C' 'D' 'F'
wanted and what class is this? A cell string? Does the first column have a meaning?
Accepted Answer
jonas
on 7 Jun 2018
You can try something like this.
data=readtable('Name.xlsx','sheet','test')
Headers=data.Properties.VariableNames(2:end);
One=Headers(data{1,2:end}==1)
Two=Headers(data{2,2:end}==1)
%etc..
3 Comments
jonas
on 12 Jun 2018
I see that you have displaced the table and inserted an 'x' in one of the columns. Matlab now reads the column with 'x' as a cell of strings instead of doubles. Unfortunately, you cannot force Matlab to read all columns as doubles, as the 'Format' option does not work for excel-files. What I have done is to only extract the part of the table that contains doubles. First, read the entire sheet and find the first row that contains the string 'One' and the last row which contains the string 'Six'. Then, extract this range and use the same code as previously.
T_raw=readtable('Name2.xlsx','sheet','Test1')
IndexTop=find(contains(T_raw.Var1,'One'))+1;
IndexBot=find(contains(T_raw.Var1,'Six'))+1;
range=['A',num2str(IndexTop),':J',num2str(IndexBot)]
T=readtable('Name2.xlsx','sheet','Test1',...
'Range',range,...
'readvariablenames',0)
Headers=T_raw.Properties.VariableNames(2:end);
One=Headers(T{1,2:end}==1)
Two=Headers(T{2,2:end}==1)
ugly solution but it works in this case
More Answers (0)
See Also
Categories
Find more on Cell Arrays 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!