Rows Into Single Column

3 次查看(过去 30 天)
Ljubisa Jurosevic
Ljubisa Jurosevic 2021-2-24
Hi guys, I have a csv/txt file that looks something like this:
Id Image Time X Y Z Heading Roll Pitch Camera Quality Line Color AccuracyXyz
0 stream_00008-000000_01000_0018263.jpg 220769.52 129326.073 6138377.004 4.758 106.658 -0.422 3.03 48 1 1 C0;0;0;0;0;0 0.015
1 stream_00008-000000_01001_0018264.jpg 220770.02 129321.873 6138378.286 4.753 106.499 -0.466 3.033 48 1 1 C0;0;0;0;0;0 0.015
2 stream_00008-000000_01002_0018265.jpg 220770.52 129317.673 6138379.568 4.749 106.339 -0.511 3.037 48 1 1 C0;0;0;0;0;0 0.015
This is just 3 rows, there is much much more.
I want to write it into another file, which will have these rows written into single column/line, something like this:
Image=stream_00008-000000_01000_0018263.jpg
Directory=0
Time=220769.519970
Xyz=129326.073 6138377.004 4.758
Hrp=106.658385 -0.421587 3.029906
Camera=0
Quality=1
Line=1
Color=C0;0;0;0;0;0
AccuracyXyz=0.015
Image=stream_00008-000000_01001_0018264.jpg
Directory=0
Time=220770.020009
Xyz=129321.873 6138378.286 4.753
Hrp=106.498687 -0.466245 3.033203
Camera=0
Quality=1
Line=1
Color=C0;0;0;0;0;0
AccuracyXyz=0.015
Do you guys have any idea about how would this be done?

回答(1 个)

Mario Malic
Mario Malic 2021-2-24
编辑:Mario Malic 2021-2-25
Hi,
This will do it.
file = 'imagelist thinned.txt';
tableOpts = detectImportOptions(file);
tableOpts.VariableTypes = repmat({'char'}, [1,14]);
tableObj = readtable(file, tableOpts);
tableObjNew = mergevars(tableObj, {'X', 'Y', 'Z'}, 'NewVariableName','XYZ');
tableObjNew = mergevars(tableObjNew, {'Heading', 'Roll', 'Pitch'}, 'NewVariableName','HRP');
data = reshape(table2cell(tableObjNew)', [], 1);
% The merged variables are in a cell that contains 1x3 cell
% so we merge them into one cell
dataToModify = cellfun(@iscell, data);
data(dataToModify) = cellfun(@(x)strjoin(x, ' '),data(dataToModify),'UniformOutput',false);
% Assembling the information about cells
cellInf = repmat({'Image', 'Directory', 'Time', 'Xyz', 'Hrp', 'Camera', 'Quality', 'Line', ...
'Color', 'AccuracyXyz'}', [height(data)/10, 1]); % total entries are height(data)/variables
eqSigns = repmat({'='}, [height(data), 1]);
% join works with same variable types, so had to change the approach
dataComplete = join([cellInf, eqSigns, data]);
writecell(dataComplete, 'testtable.txt');
  2 个评论
Ljubisa Jurosevic
Ljubisa Jurosevic 2021-2-25
Hi Mario,
This is good but unfortunately I might didn't explain it good enough. This only combined x y z into one column. As you can see in the example that I posted, one row is the information for one image. It needs to be written into column. Than, info for other image (2nd row) needs to be written into same column, so it would be a sequel. That should be done for all images.
Mario Malic
Mario Malic 2021-2-25
I should've been more careful reading the question, now it should be good.

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by