fprintf command reading my input matrix by column and displays incorrectly.

5 views (last 30 days)
Essentially I am trying to take input data for a structure and print it to a text file. When using fprintf Matlab reads the data by the column and inputs it incorrectly to what I need. I know it's supposed to do that, but my question is how would I code around it to get the matrix to show up correctly on my data sheet. Here is the relevant code along with the text file output.
COORD = [0 0;5 0;0 5]*1000;
fileID = fopen('DataSheet.txt','w');
fprintf(fileID,'%10s %15s\n','x-coord','y-coord');
fprintf(fileID,'%10d %15d\n',COORD);
fclose(fileID);
I know the fix is probably very siimple but for the life of me I can't find what I'm doing wrong online. Any help would be appreciated.
Thank you

Answers (1)

Stephen23
Stephen23 on 26 Nov 2022
Edited: Stephen23 on 26 Nov 2022
fprintf(fileID,'%10d %15d\n',COORD.');
% ^^ transpose
"I can't find what I'm doing wrong online"
The FPRINTF documentation states on the first line "...applies the formatSpec to all elements of arrays A1,...An in column order ...", if you want it to apply the format in row order then just transpose the input matrix.

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!