Reshape non-even length columns into text output

2 次查看(过去 30 天)
Hi All-
I'm trying to convert some data from one form to another, and have run into trouble. I have a large matrix of data (257 x 257). For each row, I need to write space-separated output so that the first 10 values are on one line, the next 10 on another, etc. This would be fairly easy to do using reshape, except when I get to the last line there are only 7 values remaining, so matlab returns an error.
For example, if I had:
data = [1:13; 14:26];
I would want my output from dlmwrite to be:
1 2 3 4 5 6 7 8 9 10
11 12 13
14 15 16 17 18 19 20 21 22 23
24 25 26
Any advice? I'm using R2011a. Thanks in advance! -sam

采纳的回答

Image Analyst
Image Analyst 2013-4-11
You can do custom stuff like that easily with fprintf().
  1 个评论
Sam Zipper
Sam Zipper 2013-4-12
Thanks, in case any else wonders, here's what I ended up with:
[m,n] = size(data);
for row = 1:m;
for col = 1:10:n;
if (col+9 <= n) % if there are more than 10 data points left
fprintf(fileID, ' %9.4e', data(row,col:col+9));
fprintf(fileID, '\n');
else % if there are less than 10
fprintf(fileID, ' %9.4e', data(row,col:n));
fprintf(fileID, '\n');
end
end
end

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by