Borrar filtros
Borrar filtros

new girl needs a little help with fprintf and loopies

2 visualizaciones (últimos 30 días)
Tabitha
Tabitha el 3 de Sept. de 2013
can somebody please help me
i am VERY VERY new to matlab, but i'm trying to learn it.
i've written a script that uses a couple of "for loops" to produce several "blocks" of data, like a hundred seventy five or more blocks (only a couple blocks are shown below). when I run the script all hundred seventy or so go across my command window, but that's not what ends up in my output file. only the last block of data ends up in my output file, and i need them all in the file.
so here's an example of the output file
1 3.23 -422 21.1
1 3.25 -124 45.0
1 2.99 -378 33.3
2 3.12 -601 61.0
2 3.22 -227 41.7
2 4.02 -327 32.9
3 2.70 -129 61.8
3 1.91 -304 65.2
3 0.99 -198 63.6
.
.
.
175 7.32 -201 111.0
175 6.25 -287 101.7
175 8.02 -327 135.2
and here's the stuff I use to write the data to a file
fid = fopen('output_file.dat', 'wt')
data_to_print = [block_number data_A data_B data_C]
fprintf(fid, '%d %0.6f %d %0.6f' data_to_print)
but all i get is the last block in my output file :(
175, 7.32, -201, 111.0
175, 6.25, -287, 101.7
175, 8.02, -327, 135.2
can one of you guys please help me learn how to write all the blocks of data into the file?
thanks so much Tabby

Respuesta aceptada

Tabitha
Tabitha el 3 de Sept. de 2013
Editada: Tabitha el 3 de Sept. de 2013
Thanks guys! with your thoughts I got it :) I had to put the fprintf stament inside of three loops, 1 for the rows, 1 for the cols, and 1 for the block_number.
% code
[row,col, block_number] = size(calculated_data_from_above);
for b=1:block_number
fid = fopen('output_file.dat', 'wt');
for i= 1:col
for n = 1:row
equations to generate data_A data_B data_C
data_to_print = [ block_number data_A data_B data_C ];
fprintf(fid, '%d %0.6f %d %0.6f\n',data_to_print);
end
end
end
fclose all;
%code
I'm not really sure why all of this works (parts I undrstand, other parts I got from you guys) but just kept playing around with it till it ran, yea :)
hopefully I can use some of this for my next MatLab challenge, thanks guys, Tabby

Más respuestas (1)

Azzi Abdelmalek
Azzi Abdelmalek el 3 de Sept. de 2013
Editada: Azzi Abdelmalek el 3 de Sept. de 2013
Your for loop should be inside {fopen fclose}
fopen
%your for loop
fclose

Categorías

Más información sobre Startup and Shutdown en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by