How to build multiple column vector output arguments by indexing from several single column arrays
3 views (last 30 days)
Show older comments
I have written a simple piece of code that opens a csv file and orders the columns of interest into seperate single column arrays. The code uses the csvimport function posted on the file exchange:
if exist('PARAMETERS_0.csv', 'file') == 2
disp('file found')
[PX PY PZ RX RY RZ] = csvimport('PARAMETERS_0.csv', 'columns', {' PositionX', ' PositionY', ' PositionY', ' RotationX', ' RotationY', ' RotationZ'});
else
disp('camera parameters input file not found')
end
The output arrays represent x, y, z, components of vectors (PX, PY, PZ) and euler rotations around x, y, z, axes (RX, RY, RZ). E.g:
PX =
0.5053
0.5300
0.3820
-0.1594
-0.1289
PY =
1.4532
1.4258
1.1240
1.2185
1.2424
PZ =
-0.3920
0.0887
0.3791
-0.4576
-0.0259
(note that true output arrays a far longer than indicated above)
My problem is that I need to sequentially index each row of the PX, PY, PZ and RX, RY, RZ and reassign these values to discrete column vectors. For the above dataset the output should look as follows:
PV1 =
0.5053
1.4532
-0.3920
PV2 =
0.5300
1.4258
0.0887
PV3 =
0.3820
1.1240
0.3791
And so on. I can create the column vectors on an individual basis using the following command:
PV1 = [PX([3]); PY([3]); PZ([3])]
However, I need to write a function that with sequentially perform an equivalent command, and sort the single column arrays into discrete column vectors (I am bemused by this task at present!). Note that whilst all the single column arrays from one .csv file will always have an equal number of rows, the number of rows will vary between files and will represent an unknown.
Any help or suggestions would be greatly appreciated.
Thomas
0 Comments
Accepted Answer
Julien
on 8 Oct 2012
Edited: Julien
on 8 Oct 2012
Hi,
It exists many solutions to define sequential names of variables (PV1 PV2 PV3 ...) inside a for loop.
The most simple is the eval function, but it seems that we should avoid the use of this function.
The best solution is to store all inside a matrix, each column corresponding to one variable.
But if you want absolutely to store your datas in separated variables names, i'll do this:
for i=1:length(PX)
assignin('base',['PV' num2str(i)],[PX(i);PY(i);PZ(i)])
end
but you cans store all inside a matrix, each PV corresponding to one column
for i=1:length(PX)
PV(:,i)=[PX(i);PY(i);PZ(i)];
end
(if I have well understood what you want to do)
EDIT: I've just read again your question and I saw : note that true output arrays a far longer than indicated above
You should store all inside a matrix. If all your datas has the same number of element per file, that is the best way to avoid troubles ( and it is very easier to access matrix column within a loop than using sequential names)
5 Comments
More Answers (0)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!