Separate CSV to stucture array by a specified field
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Andrey Kazak
el 18 de Oct. de 2015
Comentada: Guillaume
el 20 de Oct. de 2015
Greetings!
I have a CSV file with the following contents:
"x", "y", "ZLEVEL"
15566.071598095354, 654.75138415898618, A
15730.023876579013, 660.38875580774038, A
15772.038140720921, 665.0524486236709, A
15861.67702561093, 669.09878141965692, B
15964.77015716501, 675.05706329613804, B
16054.020545106847, 683.1063847011992, B
16118.583970776759, 690.87616533615892, B
16231.136948002502, 695.99923622422557, C
16344.66294729244, 700.25827295850286, C
16400.372270382126, 705.20613829250306, C
16486.242397353868, 709.25627232144143, C
16564.589258300024, 715.96303745505884, C
16624.046537611983, 723.13271402537612, C
16752.728644676041, 727.80842778700935, C
16860.248868173338, 732.05753736629867, C
16906.759450074052, 736.81366143388732, C
I intent to create a 3 by 1 struct array with two fields XYData (matrix) and ZLEVEL (string), which holds the CSV data separated by ZLEVEL field and XYData matrix holds X and Y in its original CSV order.
Can you suggest me a nifty way (using built-in MATLAB functions) to do this please?
Thank you.
0 comentarios
Respuesta aceptada
Walter Roberson
el 19 de Oct. de 2015
If you have R2014b or later you can use tableread() and then group the entries by ZLevel.
But that's because you asked for nifty, not for efficient.
2 comentarios
Más respuestas (1)
Guillaume
el 19 de Oct. de 2015
A table probably makes the reading of the file easier but I'm not sure it helps much after that. I don't think unstack is going to work for this.
It's still somewhat simple to do:
%read data with textscan
fid = fopen('somefile.txt', 'rt');
c = textscan(fid, '%f, %f, %s', 'HeaderLines', 1);
fclose(fid);
%or as a table
t = readtable('somefile.txt');
%partition the data,
%using a table here. replace t.x, t.y, t.ZLEVEL, by c{1}, c{2}, c{3} if using textscan
[levels, ~, pos] = unique(t.ZLEVEL); %get partitioning
xygrouped = accumarray(pos, 1:numel(pos), [], @(v) {[t.x(v), t.y(v)]}) %defer to accumarray to do the grouping
sarray = struct('XYData', xygrouped, 'ZLEVEL', levels) %and convert to struct
2 comentarios
Guillaume
el 20 de Oct. de 2015
I thought your objective was to create a structure. Note that my example does use a table object.
Your step 1) and 2) are the same as my unique line.
Your step 3) is the same as my accumarray line.
Ver también
Categorías
Más información sobre Cell Arrays en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!