extracting data from a file and creating a file.

조회 수: 7 (최근 30일)
Yeliz ALP
Yeliz ALP 2022년 10월 17일
답변: Amit Dhakite 2023년 6월 8일
How should the matlab code be, which reads the times corresponding to the given station names (for example C0AU3SZ IP, c0ATYSZ IP ....)from the data files we have and save them into a single file. The sample file is as follows.
  댓글 수: 1
Rik
Rik 2022년 10월 17일
Have a read here and here. It will greatly improve your chances of getting an answer.

댓글을 달려면 로그인하십시오.

답변 (1개)

Amit Dhakite
Amit Dhakite 2023년 6월 8일
Hi Yeliz,
According to my understanding, you want to read station names and their corresponding times from multiple files.
You can refer to the following example to get started, which reads data from one file "data_in.txt":
And saves the required information in the file "data_out.txt":
Here is the code snippet:
% Open the text file
fid = fopen('data_in.txt', 'r');
% Read the first line and split station names and times
header = fgetl(fid);
header = split(header, ' ');
stations = header{1};
times = header{2};
% Initialize arrays to hold station names and times
stationNames = {};
stationTimes = {};
% Loop through the rest of the lines and extract station name and time
while ~feof(fid)
line = fgetl(fid);
pieces = split(line, ' ');
stationNames{end+1} = pieces{1};
stationTimes{end+1} = pieces{2};
end
% Close the file
fclose(fid);
% Save the station names and times to a new file
fidNew = fopen('data_out.txt', 'w');
fprintf(fidNew, '%s %s\n', stations, times);
for i=1:length(stationNames)
fprintf(fidNew, '%s %.2f\n', convertCharsToStrings(stationNames{i}), convertCharsToStrings(stationTimes{i}));
end
% Close the new file
fclose(fidNew);
For further information related to the functions used above, kindly refer to the following links:
  1. fopen(): https://www.mathworks.com/help/matlab/ref/fopen.html
  2. fgetl(): https://www.mathworks.com/help/matlab/ref/fgetl.html
  3. split(): https://www.mathworks.com/help/matlab/ref/split.html
  4. feof(): https://www.mathworks.com/help/matlab/ref/feof.html
  5. fclose(): https://www.mathworks.com/help/matlab/ref/fclose.html
  6. fprintf(): https://www.mathworks.com/help/matlab/ref/fprintf.html
  7. convertCharsToStrings(): https://www.mathworks.com/help/matlab/ref/convertcharstostrings.html

카테고리

Help CenterFile Exchange에서 Text Data Preparation에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by