Filter löschen
Filter löschen

Reshape non-even length columns into text output

2 Ansichten (letzte 30 Tage)
Sam Zipper
Sam Zipper am 11 Apr. 2013
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

Akzeptierte Antwort

Image Analyst
Image Analyst am 11 Apr. 2013
You can do custom stuff like that easily with fprintf().
  1 Kommentar
Sam Zipper
Sam Zipper am 12 Apr. 2013
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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by