Find a specific value in a csv file
Show older comments
How can I make it?
In csv file, I want to find the year in Part 1 and D value ("D":"1") in Part2
Finally I want to make :
2022, 1
2021, 2
2020, 3
2019, 4
2018, 5
Answers (2)
Maybe this will work on your file (if not, upload the file here)
file_name = 'table_data.csv';
C = readcell(file_name,'Delimiter',',','NumHeaderLines',1);
years = regexp(C(:,1),'(\d{4})','tokens','once');
years = vertcat(years{:})
d = regexp(C(:,end),'D:"(\d+)"','tokens','once');
d = vertcat(d{:})
% result as a cell array of character vectors:
result = [years d]
% result as a numeric matrix:
result = str2double([years d])
9 Comments
Alexai
on 16 Jul 2022
file_name = 'table_data.csv';
C = readcell(file_name,'Delimiter','\t','NumHeaderLines',1)
years = regexp(C(:,1),'/(\d{4})','tokens','once');
years = vertcat(years{:})
d = regexp(C(:,end),'"D":"(\d+)"','tokens','once');
d = vertcat(d{:})
% result as a cell array of character vectors:
result = [years d]
% or, result as a numeric matrix:
result = str2double([years d])
Voss
on 16 Jul 2022
I'm not sure what you mean by "read next tab", because the file is read using tab as the delimiter:
file_name = 'table_data.csv';
C = readcell(file_name,'Delimiter','\t','NumHeaderLines',1)
so there are no tabs in C:
contains(C,sprintf('\t'))
However, if you want to get the part between the last slash and the end (which corresponds to where the tabs are in the file), of each character vector in the first column of C, you can do this:
% match a slash followed by any non-slash characters, occurring at the end
% (to prevent matching previous slash+stuff parts)
years = regexp(C(:,1),'/([^/]*)$','tokens','once');
years = vertcat(years{:})
And to make the regular expression for the "D" part more general (allow matching anything between the double-quotes, not just digits), you can do this:
% match the literal "D":" and return any characters after that, before the next '"'
d = regexp(C(:,2),'"D":"(.*?)"','tokens','once');
d = vertcat(d{:})
More about Regular Expressions: Operators and Characters in Regular Expressions
Another way to use regular expressions to achieve the same result would be to operate on the contents of the file directly, i.e., not on a cell array that comes from readcell.
Here's how that would work:
file_name = 'table_data.csv';
fid = fopen(file_name);
data = fread(fid,'*char').'; % read the file as a character vector
fclose(fid);
disp(data);
% match a slash, followed by any non-slash non-tab characters, followed by
% a tab, and return the stuff between the slash and the tab
% (note: not using 'once' this time because this regexp operates on the
% file's entire contents)
years = regexp(data,'/([^\t/]*)\t','tokens');
years = vertcat(years{:})
% match the literal '""D"":""', return the characters after that, before the next '""'
d = regexp(data,'""D"":""(.*?)""','tokens');
d = vertcat(d{:})
Alexai
on 17 Jul 2022
Voss
on 17 Jul 2022
Try to adapt some of the examples I've given. Refer to the link I shared.
file_name = 'table_data.csv';
C = readcell(file_name,'Delimiter','\t','NumHeaderLines',1)
years = regexp(C(:,1),'/([^/]*/[^/]*)$','tokens','once');
years = vertcat(years{:})
Hi!
Try this:
% Create, split, and extract from part 1
part1 = "A/1/2/" + string(2022:-1:2018) ;
part1 = split(part1, '/') ;
yrs = part1(:,:,end) ;
% Create, split, and extract from part 2
part2 = " ""B"":""1"", ""C"":""2"", ""D"":""" + string(1:5) + '"' ;
part2 = split(part2, '"') ;
dig = str2double(part2(:,:,end-1)) ;
% Result
result = transpose(yrs + "," + dig )
Use datetime if you want to convert this string array to date time array.
Please keep in mind this a way from many to solve it, you can also use regular experssion or patterns to do this.
Categories
Find more on Workspace Variables and MAT Files 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!