I want to extract digits from a cell array but not the digit zero

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 Commenti

It's not clear how the data is stored.
Please attach your data, using the paperclip button.
Hi:
t = readtable(filename); filename come from a csv file (excel)
@PEDRO ALEXANDRE Fernandes: please upload the original data source (file, URL, ...). Most likely it can be imported better.
Hi Stephen.
I only have that file.
Sorry
"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.
Hi Stephen.
The file they gave me already had this name. I believe they must have taken it from somewhere, but I don't know where.
The number of zeros is not the same in all lines.
Yes, you are right. When I said the first cell, I meant cell line.

Accedi per commentare.

 Risposta accettata

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}
ans = 1×32
3 5428 2 3 600 44 96 78 8 76 7 129 8174 83 77 9 33 8 180 1 11 10 10 3 3 1 1 1 1 1

11 Commenti

Hi Jan..
But if I want to extract the first digits of all rows?
Your code only give me the first line.
But if I want the digits for each line?
I´m sorry, but I am inexperienced in programming in matlab
"Your code only give me the first line."
No, Jan's code gave the full result, but only showed for the first row.
result{k}
This line of code shows the kth element of the result variable, corresponding to the kth row.
You can see the all the values here -
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
result = 9×1 cell array
{[ 3 5428 2 3 600 44 96 78 8 76 7 129 8174 83 77 9 33 8 180 1 11 10 10 3 3 1 1 1 1 1 1 1]} {[ 767 2584 10 10626263 950643 25 6 24628 2 4879 0 2 11946 15 224 20 7 19 19 9 9 1 1 1 1 1 8 1]} {[ 544 5223 301 514 6 585 3191625 0 7222 31747 96 50 7 76 4 468 2 13 16 12 12 3 3 1 1 1 2 1 2 1]} {[ 669772 626 9 7 6 61 13 248 0 8 1 152 0 56629 27769 82 17 250 1 15 14 14 2 2 1 1 1 1 2 3 1 1]} {[ 434 884 45 691 0 33 765 61794007 0 8 9 590 55 88 3 89859 7 252 3 16 45 15 15 4 4 1 1 1 3 1 2 1]} {[ 9 26 76 17 1848 3 42 44 6 728 34 2 17 9 857 5 1 1 5 14 224 20 7 19 19 9 9 1 1 1 1 1 8 1]} {[600 63 6 331 4 6 2 6587 7 5 9 45 774 26692 4691191 61 78 2048 291 1 27 3 29 29 6 6 3 3 3 1 2 3 2]} {[ 2 3 1 66 5 8.3915e+10 44 0 35 31122 8 1 60 79708 538 10 6 250 1 15 14 14 2 2 1 1 1 1 2 3 1 1]} {[ 97 17003 62 8 1 74841777 6 10578718 72 9 21 26 0 5 4 2 256 1 15 14 14 2 2 1 1 1 1 2 3 1 1]}
But if I want the first digit of each cell in each row, separated by rows? do I have to convert the cell array into some matrix?
Do you mean like this?
data = readcell('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1291690/Extract.xlsx');
nRow = height(data);
result = zeros(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 number
result(k)=n(1);
end
result
result = 9×1
3 767 544 669772 434 9 600 2 97
I only want the first digit of 767 and so on
You mean the first digit of every row?
For example:
3 7 5 6
..
..
..
for each row
Hi again
When I compile the code it only gives me the first digit of the numbers in the last line.
But what I want is the first digit of all lines:
for i=1:nRow
calculate_first_digit=cellfun(@(v)v(1),""+result{i})-'0';
end
it returns only:
Columns 1 through 14
9 1 3 6 8 1 7 6 1 5 7 9 2 2
Columns 15 through 28
5 4 2 2 1 1 1 1 2 2 1 1 1 1
Columns 29 through 32
2 3 1 1

Accedi per commentare.

Più risposte (1)

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 '
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.
ca = 46×1 cell array
{'3' } {'5428'} {'2' } {'3' } {'600' } {'44' } {'96' } {'78' } {'8' } {'76' } {'7' } {'129' } {'8174'} {'83' } {'77' } {'9' } {'33' } {'8' } {'180' } {'1' } {'11' } {'0' } {'0' } {'0' } {'0' } {'10' } {'10' } {'3' } {'3' } {'1' }
% Turn into a numerical vector
for k = 1 : numel(ca)
vnc1(k) = str2double(ca{k});
end
vnc1 % show in command window
vnc1 = 1×46
3 5428 2 3 600 44 96 78 8 76 7 129 8174 83 77 9 33 8 180 1 11 0 0 0 0 10 10 3 3 1

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by