Borrar filtros
Borrar filtros

Reshape non-even length columns into text output

2 visualizaciones (últimos 30 días)
Sam Zipper
Sam Zipper el 11 de Abr. de 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

Respuesta aceptada

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

Iniciar sesión para comentar.

Más respuestas (0)

Community Treasure Hunt

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

Start Hunting!

Translated by