how to write a for loop for 4 screws which have x,y,z so that it get print

Respuestas (1)

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

Thanks Garmit
Garmit Pant. your code works but when i implement it says error, that error using fprintf,function is not defined for 'cell' inputs
Can please provide the code you are running along with the arguments that you are passing to fprintf?
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
PA el 4 de Jul. de 2022
Editada: Image Analyst el 4 de Jul. de 2022
Yes they are cells, so how can I proceed?
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.
it worked but it takes the value of x for all the the three screw x, screwy, screw z
thank you:)

Iniciar sesión para comentar.

Categorías

Preguntada:

PA
el 4 de Jul. de 2022

Comentada:

PA
el 5 de Jul. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by