How to get data after a specific word in the form of cell arrays

조회 수: 1 (최근 30일)
Ali Ra
Ali Ra 2018년 12월 29일
댓글: Image Analyst 2018년 12월 30일
Hello guys
I have a txt file containing a lot of "include" word, and I need all data after "include" word for example in first row after "include" i want the numbers after "include:" in an array and if these numbers are not between -6.5 and 6.5 delete them and put the remaining numbers in an array.then put the -6.5 , 6.5 , sin(x^2) and diff in an 2*4 cell array is named myArray and when i call the myArray{1}{1} it gives me -6.5.and when it's done, go to the next "include" .thanks a lot for your help.
include:-5 -20 -6 -8 10 12 16
-6.5 6.5
sin(x^2)
diff
Some text written(We have a different sentence between each "include" word)---
include:7 2 -5 -4 -7 -9 -3 20
-5 5
cos(x^2)
diff
Some text written(We have a different sentence between each "include" word)---
  댓글 수: 2
Stephen23
Stephen23 2018년 12월 29일
@Ali Ra: please upload a sample text file by clicking the paperclip button.

댓글을 달려면 로그인하십시오.

답변 (2개)

per isakson
per isakson 2018년 12월 29일
편집: per isakson 2018년 12월 29일
I'm stuck! (I need a more detailed instruction in a list format.)
%%
str = fileread( 'SampleText.txt' );
pos = strfind( str, 'include:' );
cac = regexp( str(pos(1):end), 'include:', 'split' );
cac(1) = [];
%%
for buf = cac
block = strsplit( buf{1}, '\r\n' );
x = sscanf( block{1}, '%f' );
lim = sscanf( block{2}, '%f' );
exp = strtrim( block{3} );
exp = strrep( exp, '^', '.^' );
fcn = strtrim( block{4} );
x( x<lim(1) | x>lim(2) ) = [];
y = eval(exp);
z = feval( fcn, y );
end
Matlab doesn't recognize int and self
diff of what?
  댓글 수: 4
per isakson
per isakson 2018년 12월 30일
편집: per isakson 2018년 12월 30일
"You should have a look at vectorize()" Indeed! I've missed that function for all those years.
Please replace
exp = strtrim( block{3} );
exp = strrep( exp, '^', '.^' );
by
exprn = strtrim( block{3} );
exprn = vectorize( exprn );
and
y = eval(exp);
by
y = eval(exprn);
exprn is a better name of a variable. exp() is a function of Matlab.
per isakson
per isakson 2018년 12월 30일
편집: per isakson 2018년 12월 30일
@Ali Ra
You write "[...] when i [sic] run the code it gives me this error:". Obviously, you have tried the code of my answer, but ignored the rest. I know that the code throws errors and that assigning the result to myArray is still missing.
Why don't you step through my code in debug mode and tell me what is correct and what is wrong. As I said, I'm stuck. See: Debug a MATLAB Program and Examine Values While Debugging
And you must tell us the meaning of int and self

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2018년 12월 30일
Try this. I think it does what you want:
fullFileName = fullfile(pwd, 'SampleText.txt');
% Open the file.
fileID = fopen(fullFileName, 'rt');
% Read the first line of the file.
textLine = fgetl(fileID);
count = 1;
while ischar(textLine)
fprintf('Processing line : %s\n', textLine);
if startsWith(textLine, 'include')
% Extract just the numbers part.
str = textLine(9:end)
% Get the numbers on the include line.
includeNumbers = sscanf(str, '%f ');
% Read the next line under the include line.
textLine = fgetl(fileID);
% Get the two numbers on that line.
twoNumbers = sscanf(textLine, '%f ');
% Extract only those inside the range
mask = includeNumbers >= min(twoNumbers) & includeNumbers <= max(twoNumbers);
includeNumbers = includeNumbers(mask);
% Store the "include" numbers that are in range into a cell array.
caInclude{count} = includeNumbers;
% "then put the -6.5 , 6.5 , sin(x^2) and diff in an 2*4 cell array is named myArray"
myArray{count, 1} = min(twoNumbers);
myArray{count, 2} = max(twoNumbers);
myArray{count, 3} = 'sin(x^2)';
myArray{count, 4} = 'diff';
count = count + 1;
end
% Read the next line.
textLine = fgetl(fileID);
end
% All done reading all lines, so close the file.
fclose(fileID);
celldisp(caInclude);
celldisp(myArray);
  댓글 수: 1
Image Analyst
Image Analyst 2018년 12월 30일
In case you aren't going to run the code, here is what it produces. It produces the numbers on the "include" line that are in the range on the line below it in the "caInclude" cell array. Then it makes the other cell array you wanted, called "myArray" that has
  1. the min of the range in column 1,
  2. the max of the range in column 2,
  3. sin(x^2) in column 3, and
  4. diff in column 4
caInclude{1} =
-5
-6
caInclude{2} =
2
-5
-4
-3
caInclude{3} =
2.1
1.2
0.71
1.5
2.25
3.25
2.01
caInclude{4} =
3.1
4.2
5.25
5.5
4.65
0.25
2
myArray{1,1} =
-6.5
myArray{2,1} =
-5
myArray{3,1} =
-5.5
myArray{4,1} =
-6.5
myArray{1,2} =
6.5
myArray{2,2} =
5
myArray{3,2} =
3.3
myArray{4,2} =
6.5
myArray{1,3} =
sin(x^2)
myArray{2,3} =
sin(x^2)
myArray{3,3} =
sin(x^2)
myArray{4,3} =
sin(x^2)
myArray{1,4} =
diff
myArray{2,4} =
diff
myArray{3,4} =
diff
myArray{4,4} =
diff

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by