필터 지우기
필터 지우기

new girl needs a little help with fprintf and loopies

조회 수: 2 (최근 30일)
Tabitha
Tabitha 2013년 9월 3일
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

채택된 답변

Tabitha
Tabitha 2013년 9월 3일
편집: Tabitha 2013년 9월 3일
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

추가 답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2013년 9월 3일
편집: Azzi Abdelmalek 2013년 9월 3일
Your for loop should be inside {fopen fclose}
fopen
%your for loop
fclose

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by