I want to extract digits from a cell array but not the digit zero
조회 수: 3 (최근 30일)
이전 댓글 표시
Hi..
I have a problem.
I have a cell array and want to extract digits from each cell without the zeros.
For example:
first cell of table = aedaf3c5428a2e3ba600c44b96ad78dfdf8ed76e7df129bcd8174d83b77a9c33 8 180 1 11 0 0 0 0 10 10 3 3 1 1 1 1 0 0 1 1 0 1 0 0 0 0 0 0 0
second cell of table = fe767fb2584a10c010626263ea950643ac25f6ca24628f2c4879f0c2d11946aa 15 224 0 20 7 0 0 0 19 19 9 9 1 1 1 1 0 0 0 0 0 0 1 0 0 0 8 1 0
I want this output: 3 5428 2 3 600 44 96 ... 8 180 1 11 10 10 3 3 ...
for all cells.
I try to use the function extract:
vnc1 = readcell(extract(B,digitsPattern),'Range',[1 1]);
the problem is that I have a table of values and the extract function must start with a string
Then I wanted to extract from these digits only the first digit.
calculate_first_digit=cellfun(@(v)v(1),""+vnc1)-'0';
If you could help me, I would appreciate it a lot.
Pedro
댓글 수: 8
Stephen23
2023년 2월 10일
편집: Stephen23
2023년 2월 10일
"I only have that file."
Yet the filename "Extract" implies that it is an extract of some other document or file. Odd.
Question: are the number of zeros in each row the same?
Although you describe in your question "first cell of table", what you show is actually contained in multiple cells of one row of the Excel worksheet.
채택된 답변
Jan
2023년 2월 10일
data = readcell('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1291690/Extract.xlsx');
nRow = height(data);
result = cell(nRow, 1);
for k = 1:nRow
a = data{k, 1}; % 1st column: 'aedaf3c5428a2e3ba600c4...'
a(a < '0' | a > '9') = ' '; % Hide all non-digits
n = sscanf(a, '%g', [1, inf]); % Extract the numbers
m = [data{k, 2:end}]; % Other columns
m(m == 0) = []; % Remove the zeros
result{k} = [n, m]; % Join both parts
end
result{1}
댓글 수: 11
추가 답변 (1개)
Image Analyst
2023년 2월 10일
Try this:
pat = digitsPattern;
str = 'aedaf3c5428a2e3ba600c44b96ad78dfdf8ed76e7df129bcd8174d83b77a9c33 8 180 1 11 0 0 0 0 10 10 3 3 1 1 1 1 0 0 1 1 0 1 0 0 0 0 0 0 0 '
ca = extract(str, pat) % Cell array of numbers.
% Turn into a numerical vector
for k = 1 : numel(ca)
vnc1(k) = str2double(ca{k});
end
vnc1 % show in command window
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 String Parsing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!