basic code for automatic selection

조회 수: 8 (최근 30일)
Agastya
Agastya 2015년 3월 23일
답변: Image Analyst 2015년 3월 24일
I am trying to write a code that automatically select a part of code. This means that the code should go through the text file, look for key words and copy these key words with the associated value into a new file.
Please help me with that code

답변 (2개)

John D'Errico
John D'Errico 2015년 3월 23일
Like a certain notable supreme court justice, I may not be able to define an obscenely bad programming idea, but I know it when I see it.
There are surely better ways to do what you are doing. Of course, we can only see what you are asking to do, so knowing what you really want to do is a bit difficult.
I might suggest writing a function, that would return the arguments you need to generate, as a function of its inputs. This will require no more than a switch case statement inside.
Or you could write a simple class, using named constants.
So many ways to do what you seem to want to do, and to do so in a way that will not be pure hell to debug. Auto-generating custom code on the fly like that is just a bad idea.

Image Analyst
Image Analyst 2015년 3월 24일
What do you mean by "associated value"? And why not just do something like this
fid1 = fopen(inputFileName, 'rt');
fid2 = fopen(outputFileName, 'wt');
textLine = fgetl(fid1);
while ischar(textLine)
index = strfind(textLine, keyword);
if ~isempty(index)
fprintf(fid2, '%s', whatever..........
end
textLine = fgetl(fid1);
end
fclose(fid1)
fclose(fid2)
I'm sure you can figure out what to do to finish it.

카테고리

Help CenterFile Exchange에서 Workspace Variables and MAT Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by