How can I compress a table

6 views (last 30 days)
Ellen
Ellen on 28 Nov 2023
Commented: Ellen on 29 Nov 2023
I have an imported csv file. Containing a lot of rows How can I compress them so that al of the data of one year is stored in one row. (as in the picture below? I could not get the script from an much older matlab version running)
and what kind of type are the columns with the icon that looks like three sticky notes?
Kind regards Ellen
  2 Comments
Christopher McCausland
Christopher McCausland on 28 Nov 2023
Edited: Christopher McCausland on 28 Nov 2023
Hi Ellen,
The best way forward is probably to share a few rows of your data. Could you also explain a little more how you are expecing the output to look like? Sharing the old code may help here too.
In terms of the three sticky notes, I beleive they are known as 'pages', all that is being symbolised is that you have multiple data points stored in one structure field and row i.e. a 8784x1 double rather than a signal value as in the year feild. It makes a bit more sense here: Page-wise matrix multiplication - MATLAB pagemtimes - MathWorks Switzerland read the "More About" for a nice diagrame.
Hope this helps,
Christopher
Ellen
Ellen on 28 Nov 2023
Moved: Dyuman Joshi on 28 Nov 2023
This is the file and I found out how to create the first row with years.
But if i have the table put in my workspace how can I tell that I want to read te rows.
  • read_knmivliss.m is a copy of the original file but that uses de csv directly from the source.
I have changed and used so far:
%% read KNMI data station Vlissingen:
% Gedownload van http://projects.knmi.nl/klimatologie/uurgegevens/selectie.cgi
% Geselecteerd zijn: wind- en luchtdrukgegevens
% Coordinaten en terreinhoogte uit deze datafile maar
% gegevens meethoogte vanuit KNMI, luchtdruk op 1.5m +8m NNAP
meteo.data=struct('year', num2cell(1996:2022));
meteo.source='KNMI https://www.knmi.nl/nederland-nu/klimatologie/uurgegevens';
meteo.location='310 = Vlissingen 51.44144N 3.59582E, terreinhoogte: 8.0 m NAP. Meethoogte 20 meter +nap';
meteo.units='wind in m/s, direction in degrees east of north, pressure in Pa, time in UTC';
%% read KNMI data:
noy=size(meteo.data,2); % number of years
%for indecade=0:2 % files span of 3 decades
%%I need to find de code to my own as table imported csv file which is given the name KNMI31019962023
I want the in de cell in the rows the corresponding data.It has tot look similar to te picture in de first post. Should I call the file meteo.data? But when I rename the data disappear.
Kind regards Ellen

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 28 Nov 2023
Edited: Stephen23 on 29 Nov 2023
"How can I compress them so that al of the data of one year is stored in one row. (as in the picture below?"
Your screenshot shows a non-scalar structure with fields 'year', 'month', etc.:
We can get such a structure by importing as a table and then a few simple manipulations on it:
T = readtable('KNMI310_1996-2023.csv')
T = 43848×7 table
YYYY MM dd HH DD FF P ____ __ __ __ ___ __ _____ 2008 1 1 1 120 30 10272 2008 1 1 2 170 20 10271 2008 1 1 3 130 10 10266 2008 1 1 4 220 20 10261 2008 1 1 5 200 10 10260 2008 1 1 6 110 30 10257 2008 1 1 7 150 20 10253 2008 1 1 8 90 30 10252 2008 1 1 9 100 30 10252 2008 1 1 10 100 30 10252 2008 1 1 11 110 50 10249 2008 1 1 12 100 40 10245 2008 1 1 13 100 50 10240 2008 1 1 14 110 60 10235 2008 1 1 15 110 60 10236 2008 1 1 16 100 50 10234
U = varfun(@(v){v},T, 'GroupingVariables','YYYY')
U = 5×8 table
YYYY GroupCount Fun_MM Fun_dd Fun_HH Fun_DD Fun_FF Fun_P ____ __________ _______________ _______________ _______________ _______________ _______________ _______________ 2008 8784 {8784×1 double} {8784×1 double} {8784×1 double} {8784×1 double} {8784×1 double} {8784×1 double} 2009 8760 {8760×1 double} {8760×1 double} {8760×1 double} {8760×1 double} {8760×1 double} {8760×1 double} 2010 8760 {8760×1 double} {8760×1 double} {8760×1 double} {8760×1 double} {8760×1 double} {8760×1 double} 2011 8760 {8760×1 double} {8760×1 double} {8760×1 double} {8760×1 double} {8760×1 double} {8760×1 double} 2012 8784 {8784×1 double} {8784×1 double} {8784×1 double} {8784×1 double} {8784×1 double} {8784×1 double}
S = table2struct(U) % take a look at S in the variable viewer.
S = 5×1 struct array with fields:
YYYY GroupCount Fun_MM Fun_dd Fun_HH Fun_DD Fun_FF Fun_P

More Answers (1)

Peter Perkins
Peter Perkins on 28 Nov 2023
Here's another possibility, using rowfun, that returns a table containing tables, which maybe is easier to use than what you asked for:
T = readtable("https://www.mathworks.com/matlabcentral/answers/uploaded_files/1554527/KNMI310_1996-2023.csv")
T = 43848×7 table
YYYY MM dd HH DD FF P ____ __ __ __ ___ __ _____ 2008 1 1 1 120 30 10272 2008 1 1 2 170 20 10271 2008 1 1 3 130 10 10266 2008 1 1 4 220 20 10261 2008 1 1 5 200 10 10260 2008 1 1 6 110 30 10257 2008 1 1 7 150 20 10253 2008 1 1 8 90 30 10252 2008 1 1 9 100 30 10252 2008 1 1 10 100 30 10252 2008 1 1 11 110 50 10249 2008 1 1 12 100 40 10245 2008 1 1 13 100 50 10240 2008 1 1 14 110 60 10235 2008 1 1 15 110 60 10236 2008 1 1 16 100 50 10234
U = rowfun(@(MM,dd,HH,DD,FF,P){table(MM,dd,HH,DD,FF,P)},T, GroupingVariables="YYYY",OutputVariableNames="Data")
U = 5×3 table
YYYY GroupCount Data ____ __________ ______________ 2008 8784 {8784×6 table} 2009 8760 {8760×6 table} 2010 8760 {8760×6 table} 2011 8760 {8760×6 table} 2012 8784 {8784×6 table}
U.Data{1}
ans = 8784×6 table
MM dd HH DD FF P __ __ __ ___ __ _____ 1 1 1 120 30 10272 1 1 2 170 20 10271 1 1 3 130 10 10266 1 1 4 220 20 10261 1 1 5 200 10 10260 1 1 6 110 30 10257 1 1 7 150 20 10253 1 1 8 90 30 10252 1 1 9 100 30 10252 1 1 10 100 30 10252 1 1 11 110 50 10249 1 1 12 100 40 10245 1 1 13 100 50 10240 1 1 14 110 60 10235 1 1 15 110 60 10236 1 1 16 100 50 10234
U.Data{1}.DD(1:5) % etc.
ans = 5×1
120 170 130 220 200

Categories

Find more on Programming in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!