Rows Into Single Column

3 Ansichten (letzte 30 Tage)
Ljubisa Jurosevic
Ljubisa Jurosevic am 24 Feb. 2021
Kommentiert: Mario Malic am 25 Feb. 2021
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 Kommentare
KALYAN ACHARJYA
KALYAN ACHARJYA am 24 Feb. 2021
Bearbeitet: KALYAN ACHARJYA am 24 Feb. 2021
Please attach the text file
Ljubisa Jurosevic
Ljubisa Jurosevic am 24 Feb. 2021
Hello,
here it is as a csv and txt.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Mario Malic
Mario Malic am 24 Feb. 2021
Bearbeitet: Mario Malic am 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 Kommentare
Ljubisa Jurosevic
Ljubisa Jurosevic am 25 Feb. 2021
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 am 25 Feb. 2021
I should've been more careful reading the question, now it should be good.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by