Borrar filtros
Borrar filtros

Writing to file with condition

2 visualizaciones (últimos 30 días)
Yesbol
Yesbol el 5 de Abr. de 2017
Comentada: Yesbol el 6 de Abr. de 2017
I have a matrix A 335x5 and I'm extracting 2nd and 3rd column from the matrix with the corresponding number. Currently, the program looks like this:
fileID = fopen('XY_Position.txt','w');
for i=1:nodes
fprintf(fileID,'%d,%.4f,%.4f\n',i,A(i,2),A(i,3));
end
fclose(fileID);
The output is:
1,355.3035,176.9755
2,353.8089,176.1707
3,352.1992,175.4808
4,350.7045,174.9059
5,344.1509,173.5262
6,338.0571,173.0663
7,332.8832,172.9513
8,330.2387,172.4914
etc
Now I need to include a condition where if, A(current row,4) Not Equal to A(next row,2), then extract elements A(current row,4) and A(current row,5). Please refer to the example below to understand the problem better.
For instance, we have a matrix:
22 305.2889 146.0469 306.5536 142.3677
23 306.5536 142.3677 301.6096 140.2981
24 301.6096 140.2981 299.0802 139.0333
25 299.0802 139.0333 297.0106 138.2285
26 518.2249 225.1505 507.1871 230.3245
27 507.1871 230.3245 505.0026 236.7631
28 505.0026 236.7631 503.5079 239.9825
As can be seen, the element A(current row, 4) is equal to A(next row,2), EXCEPT row 25 and 26.
The output is expected to be:
22,305.2889,146.0469
23,306.5536,142.3677
24,301.6096,140.2981
25,299.0802,139.0333
26,297.0106,138.2285
27,518.2249,225.1505
28,507.1871,230.3245
29,505.0026,236.7631
Please ask questions if problem is not clear enough. Your help is greatly appreciated.
Thank you.

Respuesta aceptada

Guillaume
Guillaume el 5 de Abr. de 2017
I would just construct the matrix to be printed according to your rule, then write it in one go to file. If I understood correctly:
A = [22.0000 305.2889 146.0469 306.5536 142.3677
23.0000 306.5536 142.3677 301.6096 140.2981
24.0000 301.6096 140.2981 299.0802 139.0333
25.0000 299.0802 139.0333 297.0106 138.2285
26.0000 518.2249 225.1505 507.1871 230.3245
27.0000 507.1871 230.3245 505.0026 236.7631
28.0000 505.0026 236.7631 503.5079 239.9825];
B = sortrows([A(:, 1:3); A(A(1:end-1, 4) ~= A(2:end, 2), [1, 4, 5]) + [0.5, 0, 0]]); %requires R2016b or later
B(:, 1) = B(1, 1) + (0 : size(B, 1)-1);
csvwrite('XY_Position.txt', B);
  5 comentarios
Guillaume
Guillaume el 6 de Abr. de 2017
As expected, works without issues for me with this new matrix.
You are using R2016b or R2017a, right? On R2016a or earlier the first addition would result in 'matrix dimension must agree error'. In which case,
B = sortrows([A(:, 1:3); bsxfun(@plus, A(A(1:end-1, 4) ~= A(2:end, 2), [1, 4, 5]), [0.5, 0, 0])]); %any version
would fix the problem. But then, the demo wouldn't have worked either.
Yesbol
Yesbol el 6 de Abr. de 2017
It's working! Thank you!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Structures en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by