Import Data from Text file

조회 수: 7 (최근 30일)
Abi Waqas
Abi Waqas 2015년 11월 21일
댓글: Abi Waqas 2015년 11월 24일
Hello,
I am attaching the text file. In the attached text file (Straight.txt) I have to do the following
1. I want to remove the all lines above this line "BEGIN (General Parameters ; GP)"
2. Store the variable name '_rOptical' and its value '1' in the in MATLAB variable which I will later export to Excel.
and I have to do this for whole file strating from "BEGIN (General Parameters ; GP)" to the end of the file.
After processing the output should look like the attached excel file (sampl.xls) or like given below
I think the straight file is written in any language which I don't know if anyone knows any other way please help. I will be very greatful
GP_rOptical 0
GP_rGeometrical 1
GP_eGeometricalLength 1000
Can any one help in this ?
I Will be really thankful
Abi
  댓글 수: 2
Geoff Hayes
Geoff Hayes 2015년 11월 22일
Abi - you haven't attached your text file. Once you have chosen it, you must press the Attach File button.
Abi Waqas
Abi Waqas 2015년 11월 22일
I want to add some more detail. After processing the result should like
GP_rOptical 0
GP_rGeometrical 1
GP_eGeometricalLength 1000
GP_cAttenuation 0
GP_cDispersion 0
GP_cBackscatter 0
and so on. please help
Thank in advance

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

채택된 답변

Image Analyst
Image Analyst 2015년 11월 22일
Abi:
Try this. It works fine.
fid=fopen('Straight.txt');
lineNumber = 1;
firstLineFound = true;
echoLineToCommandWindow = true;
while ~feof(fid) && lineNumber < 1000 % 1000 is the failsafe condition
% Read this line.
thisLine = fgetl(fid);
if echoLineToCommandWindow
fprintf('Processing line #%d: %s\n', lineNumber, thisLine);
end
lineNumber = lineNumber + 1;
isFirstLine = strfind(thisLine,'BEGIN (General Parameters ; GP)');
if isempty(isFirstLine) && ~firstLineFound
continue; % Skip this line. It's not the first one we're looking for.
end
% If we've just found the first line, read the next line.
if firstLineFound == false;
firstLineFound = true;
% Now read the next line.
thisLine = fgetl(fid);
if echoLineToCommandWindow
fprintf('Processing line #%d: %s\n', lineNumber, thisLine);
end
lineNumber = lineNumber + 1;
end
% If we see the line "END (INPUT)" then we should bail out.
isLastLine = strfind(thisLine,'END (INPUT)');
if ~isempty(isLastLine)
break; % Time to quit.
end
% If we get to here then that means we
% have found the "starting line" - now we can process this line.
% Look for "(_" to see if it's a line we want.
parenthesisFound = strfind(thisLine, '(_');
if isempty(parenthesisFound)
% No (_ was found on this line.
continue;
end
% Crop off beginning of line.
thisLine = thisLine(parenthesisFound(1)+1:end);
ca = textscan(thisLine,'%s','delimiter', ';');
% Let's not use the hated eval,
% let's check for known variable names instead.
if strfind(ca{1}{1}, '_rOptical')
GP_rOptical = str2double(ca{1}{2})
elseif strfind(ca{1}{1}, '_rGeometrical')
GP_rOptical = str2double(ca{1}{2})
elseif strfind(ca{1}{1}, '_eGeometricalLength')
GP_eGeometricalLength = str2double(ca{1}{2})
end
% out=sprintf('GP_%s %d',ca{1},ca{2});
end
status = fclose(fid);
  댓글 수: 2
Abi Waqas
Abi Waqas 2015년 11월 22일
Dear Image Analyst
Thanks alot, I need to spend time to look into the code. I will get back after understanding the code.
Thank you so much for you so kind and long response.
Abi Waqas
Abi Waqas 2015년 11월 24일
Dear Image Analyst
Thanks for the nice progran with proper variable names. This help me alot to understand the program.
I am modifying the program according to my needs if I need any help will ask you again :)
thank alot.

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

추가 답변 (1개)

dpb
dpb 2015년 11월 22일
For that file you'll probably just as well off to process it line by line, parsing each line as needed...
fid=fopen('yourfile');
while ~feof(fid)
l=fgetl(fid);
if strfind(l,'BEGIN (General Parameters ; GP)')
l=fgetl(fid)
while ~strfind(l,'END (INPUT)')
c=textscan(l,'%*s(%s %d%*[^\n]','delimiter',';');
out=sprintf('GP_%s %d',c{1},c{2});
...
Write the output line to another file or add it to a cell string array or whatever at this point and complete the loop. When the inner loop completes, if there's only a single block then use break to quit or if there can be another, let it go 'til the feof stops it.
Radio (_rOptical; 1; Optical Length; GROUP)
  댓글 수: 10
Image Analyst
Image Analyst 2015년 11월 24일
Yes, but it all gets back to what we're saying before: your file is so non-standard and unstructured that you're basically going to have to read a line at a time and parse it yourself. So start using strfind(), fgetl(), and things like that, like I showed in my code, to get the job done.
Abi Waqas
Abi Waqas 2015년 11월 24일
Dear Image Analyst
what condition should I put to extract the information from square bracket ?

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by