Merging two arrays containing decimal points into a table

I am currently working on data stored in netCDF format.
Each variable was read into a variable (n x 1 array) using the ncread() function
Ex. var = ncread(FilePath, 'var')
These variables contain decimal points (Ex. -1.0874020).
Using these individual variables I have read in, I would like to construct a table.
Ex.
data = [var1 var2 var3];
colNames = {'var1', 'var2', 'var3'};
table= array2table(data, 'VariableNames', colNames);
However, when I join the variables together, (data = [var1 var2 var3];) for some reason the variables stored in 'data' turns into integers (Ex. -1).
How can I merge arrays containing decimal points into a table?
Thanks,

3 Comments

Show us all the code in context and the result of
whos var* data
format short;format compact
head(table)
Unless the data are not numeric, any apparent loss of precision will simply be one of how the data are shown on screen; MATLAB will not have rounded nor truncated a double in the process of catenating variables; something else is confusing the picture here...but we don't have enough enough to know just what, not having the actual data nor precise code in entirety.
i agree with @dpb here. you might also check that the variables in the netcdf aren't single precision.
table= array2table(double(data), 'VariableNames', colNames);
might be worth a quick try
Hey,
Thank you for the quick response.
I am analyzing data collected from a tag attached to an animal.
The data logger collected the three-axis specific acceleration (accel), and the three-axis magnetic field (mag).
Sorry about that, I will show some of my code here.
% File path for the netCDF files (accel, mag data)
filePath = '/Users/~/filename.nc'
% Read the netCDF file into MATLAB
ncdisp(filePath)
% Read one of the variables from the netCDF file
order = ncread(filePath, 'order'); % This is the frequency of data collection
accel_y = ncread(filePath, 'accel_y'); % Accelerometer data from the y-axis
mag_y = ncread(filePath, 'mag_y'); % Magnetometer data from the y-axis
% Combine the arrays together
data = [order accel_y mag_y];
colNames = {'order', 'accel_y', 'mag_y'};
% Convert data into a table
accel_data = array2table(data, 'VariableNames', colNames);
For the answer to your codes, here it is:
whos var* data
% Name Size Bytes Class Attributes
% data 127516x3 1530192 int32
format short;format compact
head(accel_data)
% ans =
% 8×3 table
% order accel_y mag_y
% _____ _______ _____
% 1 -1 0
% 2 -1 0
% 3 -1 -71
% 4 -1 -71
% 5 -1 -72
% 6 -1 -72
% 7 -1 -72
% 8 -1 -72
For the array that I read the netCDF data in, the results were the following:
whos var* accel_y
% Name Size Bytes Class Attributes
% accel_y 127516x1 510064 single
The data is too large for me to show it to you here.
It seems that when I combine the two columns, the Class of data becomes int32. Is this fixable?
Thanks,

Sign in to comment.

 Accepted Answer

Aha! As we suspected, the problem is in what was NOT shown -- the order variable is apparently an int32; MATLAB will silently coerce variables of different numeric classes to the lowest common denominator -- observe
>> [int32(rand()*199) single(rand)*100 double(rand)*123456]
ans =
1×3 int32 row vector
102 75 104447
>>
That's what you've done above. Convert the individual variables to the table; there's no need to combine them into an array and then just split it back up again --
...
colNames = {'order', 'accel_y', 'mag_y'};
% Convert data into a table
accel_data=table(order,accel_y,mag_y, 'VariableNames', colNames);
...

4 Comments

dpb Thank you so much!
'the order variable is apparently an int32; MATLAB will silently coerce variables of different numeric classes to the lowest common denominator '
This makes so much more sense!
As you have mentioned, I think I will directly turn it into a table!
Thanks!
Hey dbp,
I tried the suggested code:
colNames = {'order', 'accel_y', 'mag_y'};
% Convert data into a table
accel_data=table(order,accel_y,mag_y, 'VariableNames', colNames);
However, it gives off the following error:
Error using array2table:
Invalid parameter name. Parameter name must be a nonempty string or character
vector.
accel_data = array2table(order,accel_y,mag_y, 'VariableNames', colNames);
Do you know any other way to construct a table?
Thanks,
The syntax is fine; it's in something else instead you've done that we don't have code/data for in complete context to see the whole ball o' wax...
But, if I create variables of the given types, it creates the table as shown just fine--
order=int32(1:10).';
accel_y=single(randn(size(order)));
mag_y=single(randn(size(order)));
colNames = {'order', 'accel_y', 'mag_y'};
tA=table(order,accel_y,mag_y,'VariableNames',colNames)
tA =
10×3 table
order accel_y mag_y
_____ _________ ___________
1 -0.30994 -0.59436
2 -0.58999 1.2665
3 -0.71516 -0.22809
4 1.0615 -0.00097881
5 -0.24335 1.7408
6 -0.18695 1.2983
7 -0.54987 -0.12946
8 -1.8558 1.6106
9 0.65487 -1.2138
10 -0.028611 1.8246
>>
As can see, the syntax is fine if the data are and there are no typos, etc., ...
OH! You've still called array2table instead somewhere is what caused the error -- get rid of that entirely; that's simply wrong.
accel_data = array2table(order,accel_y,mag_y, 'VariableNames', colNames);
it's table with the different variables;
Read <table> and <array2table> documentation to see the difference in functionality and syntax...
THANK YOU SO MUCH!
It worked!
So I should be using 'table' instead of 'array2table'
Thank you so much!

Sign in to comment.

More Answers (0)

Products

Release

R2020a

Asked:

on 9 Jun 2022

Commented:

on 9 Jun 2022

Community Treasure Hunt

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

Start Hunting!