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!