fopen fidr code working

조회 수: 1 (최근 30일)
rsch tosh
rsch tosh 2012년 2월 27일
답변: Hari 2025년 6월 11일
can anyone pls explain the code below. Thanks in advance.
fid_de = fopen('temp_de.txt','wt');
count = 1;
j = 1;
%write DE section
while(count <= 2*(max_xy-1))
de(1,:)=[' 110' numstr(j,' ') ' 0 0 10000 0 0 000000001' numstr(count,'D')];
fprintf(fid_de,[de(1,:) '\n']);
count = count + 1;
de(2,:)=[' 110 0 0 1 0 Line 0' numstr(count,'D')];
fprintf(fid_de,[de(2,:) '\n']);
count = count + 1;
j = j + 1;
end;
fclose(fid_de);
fidr = fopen('temp_de.txt','rt');
while 1
tline = fgetl(fidr);
if ~ischar(tline), break, end
fprintf(fidw,tline(1:80));
fprintf(fidw,'\n');
end
fclose(fidr);
id = 2*(max_xy-1);

답변 (1개)

Hari
Hari 2025년 6월 11일
Hi,
I understand that you're trying to understand the purpose and functionality of a MATLAB script that writes formatted lines to a text file (temp_de.txt) and then reads it back to write a subset of the content into another file. The script involves string formatting, file I/O, and iterative control flow.
I assume that the variable max_xy is a predefined scalar that controls how many DE section lines are written, and fidw is a valid file identifier opened for writing before the second loop starts.
In order to understand what this code is doing, you can follow the below breakdown:
Step 1: Open a new file for writing DE section
fid_de = fopen('temp_de.txt','wt');
This opens a file in text mode for writing (wt). It will be used to store the "DE section" lines.
Step 2: Initialize counters
count = 1;
j = 1;
These counters control the generation of DE lines. count likely tracks line numbers or identifiers, and j may index blocks or elements.Step 3: Loop to write formatted DE lines
while(count <= 2*(max_xy-1))
This loop generates 2*(max_xy - 1) lines. Each iteration writes two lines formatted into de(1,:) and de(2,:) using custom number-to-string formatting via numstr() (assumed user-defined or pseudocode for num2str() with formatting).
Each DE block looks like:
  • Line 1: Starts with "110", includes j, and ends with a numeric code involving count.
  • Line 2: Also starts with "110", labels it a "Line", includes the next count.
Each line is printed to file using fprintf.
Step 4: Close the file
fclose(fid_de);
Step 5: Reopen the file for reading and process each line
fidr = fopen('temp_de.txt','rt');
while 1
tline = fgetl(fidr);
if ~ischar(tline), break, end
fprintf(fidw, tline(1:80));
fprintf(fidw, '\n');
end
fclose(fidr);
This reads each line from the temporary file and writes only the first 80 characters of each line into another file associated with fidw.
Step 6: Final count assignment
id = 2*(max_xy - 1);
Stores the total number of lines written, likely for further use.Summary:
  • The code generates a formatted "DE section" and writes it to a file.
  • Each DE block consists of two structured lines.
  • The file is then reprocessed to extract only the first 80 characters from each line, possibly due to formatting standards.
Note: The function numstr() is not a standard MATLAB function; it likely mimics num2str() with padding. You may need to verify its implementation.
Refer to:
Hope this helps!

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by