how can I give name to an array column?

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

Why do you not want to work with the table? In other words, why are you extracting the data from the table (table2array)?
Its an excersize I was given.
I was given ecxel table and I need to convert to array with column names included.
this is the tabel: ECG_Signal t_sec_
__________ ______
-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
tabel2array was succesfull but it didnt keep the column names
What you're describing isn't possible in MATLAB. The only data type that contains header names is table and timetable.
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]

Sign in to comment.

Answers (1)

MATLAB has several data types.
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 = 16×2 table
ECG_Signal t_sec_ __________ ______ -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=table;
t.ECG_Signal = array(:,1)';
t.t_sec_ = array(:,2)';
new = table2struct(t)
new = struct with fields:
ECG_Signal: [-0.2440 -0.2284 -0.2108 -0.1310 -0.0402 -0.0342 -0.1046 -0.1530 -0.1886 -0.2530 -0.1938 -0.1864 -0.1456 -0.1108 -0.2252 -0.2368] t_sec_: [0 0.0400 0.0800 0.1200 0.1600 0.2000 0.2400 0.2800 0.3200 0.3600 0.4000 0.4400 0.4800 0.5200 0.5600 0.6000]
the assignment says: Using a built-in Matlab command (do not use IMPORT). In this exercise (you must take in the table and arrange in a matrix including the titles
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.

Sign in to comment.

Categories

Tags

Asked:

on 11 Jan 2023

Commented:

on 11 Jan 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!