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 comentarios

Geoff Hayes
Geoff Hayes el 22 de Nov. de 2015
Abi - you haven't attached your text file. Once you have chosen it, you must press the Attach File button.
Abi Waqas
Abi Waqas el 22 de Nov. de 2015
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

Iniciar sesión para comentar.

 Respuesta aceptada

Image Analyst
Image Analyst el 22 de Nov. de 2015

1 voto

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 comentarios

Abi Waqas
Abi Waqas el 22 de Nov. de 2015
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 el 24 de Nov. de 2015
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.

Iniciar sesión para comentar.

Más respuestas (1)

dpb
dpb el 22 de Nov. de 2015

0 votos

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 comentarios

Abi Waqas
Abi Waqas el 22 de Nov. de 2015
how to end the program ?
Image Analyst
Image Analyst el 22 de Nov. de 2015
He's not going to write the whole program for you. He gave you one example of how to parse a line and look for something using strfind(). That should be enough. You just have to modify that to look for other things. Then, finally, you end the program with a fclose(fid).
Abi Waqas
Abi Waqas el 22 de Nov. de 2015
Thanks for the reponse. my question was, how to end the execution. when I run this piece of code the execution is not stopping. I just want to run the code once to see whats happening than I can modify it accordingly. So it would be nice if know how to finish the program with example only than I will try to do further.
dpb
dpb el 22 de Nov. de 2015
The minimum is three nested end statements to close each of the loops with a final fclose
Abi Waqas
Abi Waqas el 22 de Nov. de 2015
I did the same but execution is not stopping I dont understand why. I really new with this stuff
I am really thankful for your support
fid=fopen('D:\Dropbox\PhD\BB work\BB_library\filarete\Basic Components\Straight\Text\Straight');
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});
end
end
end
fclose (fid);
Geoff Hayes
Geoff Hayes el 22 de Nov. de 2015
As dpb indicated in his answer, use a break to quit the program (put this line after the while loop has completed).
Abi Waqas
Abi Waqas el 22 de Nov. de 2015
Dear Geoff
I tired, but nothing coming out of it. variable "l" just get first line from the file and nothing else happened.
if this piece of code can give me desired result than I can start developing the code for my own purpose.
If you know anyother please or can you point any mistak please do it.
Abi Waqas
Abi Waqas el 24 de Nov. de 2015
Hi Everyone
Thanks you all for the help so far
Is there anyway that I can look for two delimeter in one time ? For example see the line below. (_eGeometricalLength; 1000; Geometrical Length [$m$m]; ?_rGeometrical )
I want the output like this. in five column, Assume this column as excel column.
A _eGeometricalLength
B 1000
C Geometrical Length
D $m$m
E ?_rGeometrical
So one delimeter is ';' and other is square bracket.
Please keep in mind ACBDE are column of excel and also that some line do contain square brackets some don't like this one
(_cBackscatter; 0; Backscatter;?_rGeometrical)
so I also need to check if there is square bracket than split it otherwise leave the other coloumn (fourth column 'D')blank. For reference Straight file is attached.
One more thing to add, only square bracket of third column (meaning third word of paranthesis (), after second semicolon)needs to be taken into account
Image Analyst
Image Analyst el 24 de Nov. de 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.
Abi Waqas
Abi Waqas el 24 de Nov. de 2015
Dear Image Analyst
what condition should I put to extract the information from square bracket ?

Iniciar sesión para comentar.

Categorías

Preguntada:

el 21 de Nov. de 2015

Comentada:

el 24 de Nov. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by