MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today
Asked by Arman Kam on 15 Feb 2013

Hi

I am getting so dizzy, i wrote a code and i want to record a table named 'DI' into a text file. I follow the instructions that is given here http://www.mathworks.com/help/matlab/ref/fprintf.html , but unfortunately it writes something quite different from the original table. I doubted that the code is wrong so i used the example that is given in the above link in my code and it gives me totally right answers.

The code that i use for this text writing is

fileID=fopen('ParkAngDI11.txt','w+'); fprintf(fileID,'%6s %12s\r\n','Time','Damage Index'); fprintf(fileID,'%6.2f %12.8f\r\n',DI); fclose(fileID);

I would be so thankful if you help me through this.

*No products are associated with this question.*

Answer by per isakson on 15 Feb 2013

Edited by per isakson on 15 Feb 2013

Accepted answer

My guess: column-wise. Change

fprintf(fileID,'%6.2f %12.8f\r\n',DI);

to

fprintf(fileID,'%6.2f %12.8f\r\n',transpose(DI));

Arman Kam on 16 Feb 2013

Thank you so much dear Per Isakson your solution worked, Can you tell me why is this happening?!

per isakson on 16 Feb 2013

**Column-wise** is the key to understand why. `fprintf` reads column-wise from the input matrix and writes "row-wise" to the file controlled by the format specification. Remember: Matlab is "column-first-oriented". Try

%% clc M = [ 11, 12; 21, 22 ] disp('-- fprintf --') fprintf( 1, '%4d,%4d\n', M )

result in the command window

M = 11 12 21 22 -- fprintf -- 11, 21 12, 22 >>

## 2 Comments

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/63621#comment_129882

What this table is containing?

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/63621#comment_129997

The Table contains somee numbersd that is irrelevant with the original table that exists in matlab.

some the datas from original table is

and it the first column continues to 39.98 and second column takes some values corresponding to column one.

the data that matlab writes for me is

i really need to write these tables to text file but i don't know what is wrong!!!