how to write a for loop for 4 screws which have x,y,z so that it get print
Mostrar comentarios más antiguos
SCREW;X;77
SCREW;Y;0
SCREW;Z;77
1 comentario
Garmit Pant
el 4 de Jul. de 2022
Can you please elaborate your question?
Respuestas (1)
Garmit Pant
el 4 de Jul. de 2022
Hello Anwesha
You can use the following code snippet to write the data that you have provided into a text file.
x = [77,-60,-1.11; 120,-60,-1.11 ; 130,-60,-1.11; 140,60,-1.11];
fileID = fopen('exp.txt','w');
for i = 1:size(x,1)
fprintf(fileID, 'SCREW;X;%4.2f\n',x(i,1));
fprintf(fileID, 'SCREW;Y;%4.2f\n',x(i,2));
fprintf(fileID, 'SCREW;Z;%4.2f\n',x(i,3));
end
fclose(fileID);
You can read more about 'fprintf' to understand how text can be written into text files: https://www.mathworks.com/help/matlab/ref/fprintf.html
8 comentarios
PA
el 4 de Jul. de 2022
PA
el 4 de Jul. de 2022
Garmit Pant
el 4 de Jul. de 2022
Can please provide the code you are running along with the arguments that you are passing to fprintf?
Garmit Pant
el 4 de Jul. de 2022
In the code that I have provided, the argument to fprintf should be a single integer/double/float value. I am still not sure about the nature of each element of tablexy but if they are cells, then this will not work. Try to convert the elements to single values before passing them to fprintf as arguements.
PA
el 4 de Jul. de 2022
Editada: Image Analyst
el 4 de Jul. de 2022
Image Analyst
el 4 de Jul. de 2022
Editada: Image Analyst
el 4 de Jul. de 2022
Maybe try tablexy{i,1} -- braces get the contents of the cell while parentheses get the cell itself.
See the FAQ:
Tables and cells operate similarly in some ways in case you really have a table variable instead of a cell array.
And try
fid = fopen('exp.txt','wt'); % Using wt instead of only w.
PA
el 5 de Jul. de 2022
PA
el 5 de Jul. de 2022
Categorías
Más información sobre Cell Arrays en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!