# Find a specific value(word) in csv file

3 Ansichten (letzte 30 Tage)
Alexai am 16 Jul. 2022
Bearbeitet: Voss am 16 Jul. 2022
In this csv file
I want to extract specific section(ex.2022) in Part 1 and "D" value(ex.1) in Part 2
Finally I want to extract it
2022 1
2021 2
2020 3
2019 4
2018 5
##### 0 Kommentare-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Antworten (1)

Voss am 16 Jul. 2022
Bearbeitet: Voss am 16 Jul. 2022
C = readcell('table_data (1).csv', ...
'Delimiter','\t', ...
'NumHeaderLines',1)
C = 5×2 cell array
{'A/1/2/AB0000/2022abc'} {'"B":"1", "C":"2", "D":"1"'} {'A/1/2/AB0000/2021abc'} {'"B":"1", "C":"2", "D":"2"'} {'A/1/2/AB0000/2020abc'} {'"B":"1", "C":"2", "D":"3"'} {'A/1/2/AB0000/2019abc'} {'"B":"1", "C":"2", "D":"4"'} {'A/1/2/AB0000/2018abc'} {'"B":"1", "C":"2", "D":"5"'}
years = regexp(C(:,1),'/(\d{4})','tokens','once');
years = vertcat(years{:})
years = 5×1 cell array
{'2022'} {'2021'} {'2020'} {'2019'} {'2018'}
d = regexp(C(:,2),'"D":"(\d+)"','tokens','once');
d = vertcat(d{:})
d = 5×1 cell array
{'1'} {'2'} {'3'} {'4'} {'5'}
% result as a cell array of character vectors:
result = [years d]
result = 5×2 cell array
{'2022'} {'1'} {'2021'} {'2'} {'2020'} {'3'} {'2019'} {'4'} {'2018'} {'5'}
% or, result as a numeric matrix:
result = str2double([years d])
result = 5×2
2022 1 2021 2 2020 3 2019 4 2018 5
##### 0 Kommentare-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Kategorien

Find more on String Parsing in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by