Filter löschen
Filter löschen

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

3 Ansichten (letzte 30 Tage)
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 Kommentare
Stephen23
Stephen23 am 10 Feb. 2023
Bearbeitet: Stephen23 am 10 Feb. 2023
"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.
PEDRO ALEXANDRE Fernandes
PEDRO ALEXANDRE Fernandes am 10 Feb. 2023
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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Jan
Jan am 10 Feb. 2023
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 Kommentare
PEDRO ALEXANDRE Fernandes
PEDRO ALEXANDRE Fernandes am 10 Feb. 2023
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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Image Analyst
Image Analyst am 10 Feb. 2023
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

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by