Rows Into Single Column
3 views (last 30 days)
Show older comments
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?
2 Comments
Answers (1)
Mario Malic
on 24 Feb 2021
Edited: Mario Malic
on 25 Feb 2021
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 Comments
Mario Malic
on 25 Feb 2021
I should've been more careful reading the question, now it should be good.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!