I have a problem with writing data to file

1 view (last 30 days)
I have some data I would like to write them in csv file but I am getting erros regarding the fprintf as well as I can get the right decimal places that I wrote. [ Please see the code below ].
fid=fopen('mn21hmya2021.csv','wt');
fprintf(fid,'time_q2,reach,x_relative_hand,y_relative_hand,z_relative_hand\n');
fprintf(fid,'%.2f,%.4f,%.4f,%.4f%.4f\n', wanted_columns');
fclose(fid);
headers = {'time_q2','reach','x_relative_hand','y_relative_hand','z_relative_hand'}
write= array2table(wanted_columns, 'VariableNames', headers)
writetable(write,'mn21hmya2021.csv','Delimiter',',')
The errora are:
1- Invalid file identifier. Use fopen to generate a valid file identifier.
2- fprintf(fid,'time_q2,reach,x_relative_hand,y_relative_hand,z_relative_hand\n');

Answers (1)

Jan
Jan on 7 Dec 2021
Edited: Jan on 7 Dec 2021
The folder might be write protected. Check this in general:
[fid, msg] = fopen('mn21hmya2021.csv','wt');
assert(fid > 0, msg);
Prefer absolute path names. It is fragile to rely on the current path to be at an expected location.
  2 Comments
Hassen Mohamed Yousif Abdelatif
Thanks for replying .
That does not work and it gave me ' Permission denied' .
Jan
Jan on 7 Dec 2021
Fine. This means, that you do not have write permissions in the current folder. What is the current folder? See the output of the cd command.

Sign in to comment.

Categories

Find more on Entering Commands in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!