Import Data from Text file
이전 댓글 표시
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
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
2015년 11월 22일
채택된 답변
추가 답변 (1개)
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
Abi Waqas
2015년 11월 22일
Image Analyst
2015년 11월 22일
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
2015년 11월 22일
dpb
2015년 11월 22일
The minimum is three nested end statements to close each of the loops with a final fclose
Abi Waqas
2015년 11월 22일
Geoff Hayes
2015년 11월 22일
As dpb indicated in his answer, use a break to quit the program (put this line after the while loop has completed).
Abi Waqas
2015년 11월 22일
Abi Waqas
2015년 11월 24일
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
2015년 11월 24일
카테고리
도움말 센터 및 File Exchange에서 Text Data Preparation에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!