Extract data from a cell contains of alphabet and number
조회 수: 3 (최근 30일)
이전 댓글 표시
Hi all, I have read the contents from file and I have a cell which contians 10133x1 cell array. The cell array contains of letter and number. I have strfind function to find the position of the letter, z and I want to extract all the number below the position of z to be a 1d array. Any tips and suggestions is appreciated. I have attached the example of the data.
My code is looks like below,
inputfile = sprintf('read_data','r'); %'r' = read
fileID = fopen(inputfile,'r');
C = textscan(fileID, '%s');
loc = strfind(C{1}(:), 'z');
댓글 수: 1
채택된 답변
Stephen23
2021년 6월 10일
편집: Stephen23
2021년 6월 10일
"I have read the contents from file and I have a cell which contians 10133x1 cell array. The cell array contains of letter and number."
This does not seem like a very efficient way to import that data. I doubt that STRFIND is suitable for this task:
Here is a simple and very efficient approach which does not read the entire file as text:
[fid,msg] = fopen('Example.txt','rt');
assert(fid>=3,msg)
fscanf(fid,'%[^z]');
fscanf(fid,'z');
vec = fscanf(fid,'%f')
fclose(fid);
unique(vec) % in case anyone wonders, yes those values repeat in the file!
Alternatively, if you really want to import the filedata as text:
str = fileread('Example.txt');
vec = sscanf(regexprep(str,'^.+z',''),'%f')
unique(vec) % in case anyone wonders, yes those values repeat in the file!
댓글 수: 2
Image Analyst
2021년 6월 10일
@K3iTH, let me be even more direct. Please attach your text file with the paperclip icon. Or attach your .mat file with your cell array stored inside it.
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!