How to use fprintf with two arrays?
6 views (last 30 days)
Show older comments
Grace Pooley
on 27 Aug 2021
Commented: Grace Pooley
on 27 Aug 2021
Hi! I'm trying to coalesce two arrays (X and Y) into a text file ('XYresults.txt'). The arrays are both 643x1 and I am trying to get X as the right column, and Y as the left. This is what I have written,
fileID = fopen('XYresults.txt','w');
fprintf(fileID,'%1s %2s\n','X data','Y data');
fprintf(fileID,'%1d %2d\n','X','Y');
fclose(fileID);
but when I run the code and open XYresults.txt, the only results are:
X data Y data
88 89
Does anyone have any advice as to why more data isn't being written? Thank you!
0 Comments
Accepted Answer
the cyclist
on 27 Aug 2021
Edited: the cyclist
on 27 Aug 2021
Assuming that X and Y are variables in your workspace, you need
fprintf(fileID,'%1d %2d\n',X,Y);
rather than
fprintf(fileID,'%1d %2d\n','X','Y');
Your code is writing the ASCII values of the characters 'X' and 'Y'.
4 Comments
Image Analyst
on 27 Aug 2021
And obviously
fprintf(fileID,'%1s %2s\n','X data','Y data');
can/should be
fprintf(fileID,'X data, Y data\n');
More Answers (0)
See Also
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!