I want to extract digits from a cell array but not the digit zero
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
PEDRO ALEXANDRE Fernandes
el 10 de Feb. de 2023
Respondida: Image Analyst
el 10 de Feb. de 2023
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 comentarios
Stephen23
el 10 de Feb. de 2023
Editada: Stephen23
el 10 de Feb. de 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.
Respuesta aceptada
Jan
el 10 de Feb. de 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}
11 comentarios
Más respuestas (1)
Image Analyst
el 10 de Feb. de 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 '
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 comentarios
Ver también
Categorías
Más información sobre String Parsing en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!