how can I give name to an array column?
Show older comments
I was given an excel file that I inserted to matlab using "readtable", now I need to convert the table to an array while keeping the column names in the matrix. when using "table2array" the column name disappear.
How can keep the column name in the array?
4 Comments
Adam Danz
on 11 Jan 2023
Why do you not want to work with the table? In other words, why are you extracting the data from the table (table2array)?
daniel
on 11 Jan 2023
Adam Danz
on 11 Jan 2023
What you're describing isn't possible in MATLAB. The only data type that contains header names is table and timetable.
John D'Errico
on 11 Jan 2023
A double precision array CANNOT have elements that are strings, characters, etc. ANYTHING else but a double is out. So you cannot do what you ask to do, at least if the result would be a numeric array.
And you cannot "name" a column of an array. Although if you wanted, you could define variables named "ECG_Signal" and "t_sec_".
However, you can have a separate vector of column names, where you would store them in a cell array.
columnnames = {'ECG_Signal' , 't_sec_'};
You could even store everything in one struct, where you would convert the table into various fields of a struct.
struct.columnnames = {'ECG_Signal' , 't_sec_'};
struct.data = [an nx2 array]
Answers (1)
the cyclist
on 11 Jan 2023
1 vote
Some of them, for example the "table" and "structure" data type, can have variable names associated with them. Others, such as the purely numeric arrays like the "double" data type, cannot.
You may have been asked to do the impossible. Was this a written assignment that you can share with us? Maybe you misunderstood the instructions.
3 Comments
I would agree. Sounds like you want a structural array.
t = table;
array = [ -0.244 0
-0.2284 0.04
-0.2108 0.08
-0.131 0.12
-0.0402 0.16
-0.0342 0.2
-0.1046 0.24
-0.153 0.28
-0.1886 0.32
-0.253 0.36
-0.1938 0.4
-0.1864 0.44
-0.1456 0.48
-0.1108 0.52
-0.2252 0.56
-0.2368 0.6 ];
t.ECG_Signal = array(:,1);
t.t_sec_ = array(:,2);
t
t=table;
t.ECG_Signal = array(:,1)';
t.t_sec_ = array(:,2)';
new = table2struct(t)
daniel
on 11 Jan 2023
Adam Danz
on 11 Jan 2023
A matrix can only store numeric values.
Cell arrays can store mixed class such as a row of strings in the top row and numers in all other rows but this would be referred to as an array, specifically a cell array, but it would not be recommended in this case.
If this is the description from the assignment, it's poorly worded or misguided.
Categories
Find more on Tables 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!