Import Data from Text file
Mostrar comentarios más antiguos
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
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
el 22 de Nov. de 2015
Respuesta aceptada
Más respuestas (1)
dpb
el 22 de Nov. de 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 comentarios
Abi Waqas
el 22 de Nov. de 2015
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
el 22 de Nov. de 2015
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
el 22 de Nov. de 2015
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
el 22 de Nov. de 2015
Abi Waqas
el 24 de Nov. de 2015
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
el 24 de Nov. de 2015
Categorías
Más información sobre Text Data Preparation en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!