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

4 次查看(过去 30 天)
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

回答(1 个)

Stephen23
Stephen23 2022-11-26
编辑:Stephen23 2022-11-26
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.

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by