Import Data from Text file
4 views (last 30 days)
Show older comments
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 Comments
Geoff Hayes
on 22 Nov 2015
Abi - you haven't attached your text file. Once you have chosen it, you must press the Attach File button.
Accepted Answer
Image Analyst
on 22 Nov 2015
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);
More Answers (1)
dpb
on 22 Nov 2015
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 Comments
Image Analyst
on 24 Nov 2015
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.
See Also
Categories
Find more on Text Data Preparation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!