How to build multiple column vector output arguments by indexing from several single column arrays

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

 Respuesta aceptada

Julien
Julien el 8 de Oct. de 2012
Editada: Julien el 8 de Oct. de 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 comentarios

Hi Julian
Thanks for your response. The reason for storing as individual column vectors is that the next series of operators are intended to construct 4 x 4 matrices in homogenous coordinates from the position vectors and euler angles. I need to calculate 3 x 3 rotation matrices for each x, y, z 'point' (these are actually camera positions and orientations calculated using photogrammetry) using the euler angles as input (these describe camera orientation), then concatenate with the PV1,..,PVn column vectors and a single row array (0 0 0 1) to produce the desired output of the form:
M1,.., Mn =
[ r11 r12 r13 px; r21 r22 r23 py; r31 r32 r33 pz; 0 0 0 1]
I then need to perform a rigid transformation on the point array using a single transformation matrix (calculated by another matlab function).
Incidentally, what is the problem with using the first bit of code? Will it stretch the memory allocation? I am not on a workstation with Matlab installed at the moment, but try both of the functions tonight and let you know.
Thanks again
Thomas
Thanks for your response. The reason for storing as individual column vectors is that the next series of operators are intended to construct 4 x 4 matrices in homogenous coordinates from the position vectors and euler angles.
Yes but I don't understand why you can't store your datas inside one matrix. if you now that column i correspond to PVi, then you can access PVi just by typing PV(:,i).
So, if you store all inside a matrix PV ( 3 rows and n columns)
PV(1,i) =PXi
PV(2,i) = PYi
PV(3,i) = PZi
then it's easy to manipulate that inside a loop to do your angle calculations.
in my computer, you cannot store more than 65536 variables.
I'm a bit new to programming in matlab, but I see what you mean. I'll have a play around and see if I can make the code work with my data.
Thanks again Julian
Thomas
I've just run both codes, and they work perfectly. I will follow your advice and store the vectors in a matrix (i.e. code 2).
Thanks again Julian!
Thomas

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Productos

Preguntada:

el 8 de Oct. de 2012

Community Treasure Hunt

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

Start Hunting!

Translated by