MATLAB Answers

fprintf reading rows not working right...

3 views (last 30 days)
Hello all!
Well I'm just not getting something, I'm sure its easy, but just not figuring it out. I'm just trying to read the rows and write them into a file. so the first row of the txt file should be 2 5 25 48 53, but that is not what I'm getting, I'm gettiing 2 14 11 9 4 4, well please help! Thanks, Here is the code:
nums = [2 5 25 48 53;
14 16 18 19 9;
11 31 4 44 52;
9 12 47 48 56;
4 14 29 4 56;
4 16 36 40 3;
1 22 39 2 46;
1 12 21 9 45;
14 6 17 33 7;
2 17 20 26 4;
10 20 45 1 3;
6 7 6 27 9;
1 0 22 23 8;
7 8 38 39 8];
fileID = fopen('Example.txt','w');
fprintf(fileID,'%1i\t %1i\t %1i\t %1i\t %1i\t %1i\n',nums);
fclose(fileID);

Accepted Answer

Image Analyst
Image Analyst on 9 Aug 2014
It's going down the rows first since MATLAB is column major. You can try transposing it or just use a for loop
for row = 1 : size(nums, 1)
fprintf(fileID,'%d\t %d\t %d\t %d\t %d\t %d\n',nums(row, :));
end

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!