How to convert table to an array when the first two variables are different?

Hello I have been working with this data set for a while and cannot get matlab to read it. The data consists of 8 variables and the first 2 are different. I will post my code and the error as well as what the data looks like.
date time pressure seatemp airtemp buoyhead windspd winddir
07/24/1994 12:00:00 1018.05 28.55 30.05 39.38 3.40 38.31
07/24/1994 13:00:00 1018.20 28.85 30.50 25.31 2.80 39.71
07/24/1994 14:00:00 1018.05 29.15 29.75 12.66 4.80 24.24
My code:
T=readtable('m17cm17.nodc','FileType','text','HeaderLines',57);
%%convert to an array
A = table2array(T)
Error: Cannot concatenate the table variables 'Var1' and 'Var2', because their types are datetime and cell.

4 Comments

You forgot to attach 'm17cm17.nodc'. Come on, make it easy for us to help you, not hard.
sorry this is my first time posting, it wont let me attach because the file format is not supported. But this is what it looks like, there are 57 lines of header text. My computer only lets me read it in matlab, hope this helped
@andrea molina: that just looks like a text file. Upload it after checking the file extension to .txt. Then we can help you.
here is the file! apparently I need to change the date and time columns to time vectors because when I try to plot graphs using them from the table they come out looking weird

Sign in to comment.

 Accepted Answer

I don't understand your statement that you "cannot get matlab to read it". Based on the error message you get from table2array, you already have read the file. It's not clear exactly what variables readtable created in the table, but it's likely that you ended up with a mix of numeric and text. The error message is just saying that you can't combine the first two variables into one homogeneous array. But you already have them in a table, and combining variables with different types is the whole point of tables.
So it's not clear what you're really doing. You may be trying to combine date and time into one value, or something else. If that is what you're doing, read them both as test, concatenate them, and call datetime on the result.

7 Comments

Well I have the table but I need to convert it to an array before I start analyzing data. Do I need to create a time vector for the first two variables? would you like me to attach an image of the matlab table?
"but I need to convert it to an array before I start analyzing data"
What functions are you planning to use to analyze the data? It's possible those functions will work on a table directly, or can operate on individual variables in the table. That way you don't need to convert the table into an array or can extract a variable to a temporary, operate on it, and put the result back in the table.
I need to make a few line graphs comparing data such as wind speed to certain months and stuff like that. I tried using the variables from the table but my graphs looked off. I showed my professor and he said it is because the dates in the table are not "real numbers" and I need to convert them. Sorry if this is confusing!
Andrea, I'm not sure what you mean by that. In a recent version of MATLAB, you can do this:
Read the data file, with date and time as separate variables
>> buoyData = readtable('m17cm17.txt','HeaderLines',56,'delimiter',' ','multipledelimsasone',true);
Merge the time var and the date var into one
>> buoyData.time = datetime(buoyData.time,'Format','HH:mm:ss');
>> buoyData.Properties.VariableNames{'date'} = 'datetime';
>> buoyData.datetime.Format = [buoyData.datetime.Format ' ' buoyData.time.Format];
>> buoyData.datetime = buoyData.datetime + timeofday(buoyData.time);
>> buoyData.time = [];
>> head(buoyData)
ans =
8×7 table
datetime pressure seatemp airtemp buoyhead windspd winddir
___________________ ________ _______ _______ ________ _______ _______
07/24/1994 12:00:00 1018 28.55 30.05 39.38 3.4 38.31
07/24/1994 13:00:00 1018.2 28.85 30.5 25.31 2.8 39.71
07/24/1994 14:00:00 1018 29.15 29.75 12.66 4.8 24.24
07/24/1994 15:00:00 1018 29.45 30.35 36.56 3.6 66.43
07/24/1994 16:00:00 1017.6 29.75 30.35 18.28 3.8 32.68
07/24/1994 17:00:00 1017.1 29.9 30.05 21.09 4.4 35.49
07/24/1994 18:00:00 1016.9 29.9 30.35 22.5 4.2 29.87
07/24/1994 19:00:00 1016.4 30.05 30.65 21.09 4.6 38.31
Plot the pressure series
>> plot(buoyData.datetime,buoyData.pressure)
Is that not what you're looking for?
actually yes! that is what I have been trying to do. thank you!
now from that code how would I go about creating variables for the specific months such as July or August? would I simply put August=(181:194,1)
I don't know what that means. If you want to create a logical variable in the table to indicate which rows are August
bouyData.isAugust = (bouyData.DateTime.Month) == 8)
But there are probably other better ways to do whatever you are doing.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!