How to filter only numerical in a table?
2 views (last 30 days)
Show older comments
Pablo Jaramillo Restrepo
on 7 Feb 2016
Commented: Pablo Jaramillo Restrepo
on 8 Feb 2016
I have data organized in a column in a table like this:
'1966.csv'
'1967.csv'
'1968.csv'
'1969.csv'
'1970.csv'
'1971.csv'
'1972.csv'
'1973.csv'
'1974.csv'
'1975.csv'
How could I only extract the numerical values (1966, 1967...) and organize them in a column beside the existing one?
0 Comments
Accepted Answer
Azzi Abdelmalek
on 7 Feb 2016
s={'1966.csv' '1967.csv' '1968.csv' '1969.csv' '1970.csv' '1971.csv' '1972.csv' '1973.csv' '1974.csv' '1975.csv'}
out=cellfun(@(x) str2double(regexp(x,'\d+','match')),s)
4 Comments
Image Analyst
on 7 Feb 2016
Edited: Image Analyst
on 7 Feb 2016
But you accepted the answer. If you want tables instead of cell arrays (like you said), see my answer, which uses tables. You can probably still use Azzi's answer on "FileNames" in my answer, instead of the for loop, but you've got to extract the column of the table into a cell array first, like I did.
More Answers (1)
Image Analyst
on 7 Feb 2016
I like that you're using the new variable type of table (well maybe not so new, but since R2013b I think). Tables are a lot easier to use and are more efficient and take up a ton less memory than a cell array.
Here's how to extract the numbers from the first column of your input table, and create a new table that has that original column plus a new column for years.
% Pablo didn't give us the code to create the table variable so we need to do it ourselves.
% First make a cell array of strings"
FileNames = {...
'1966.csv';
'1967.csv';
'1968.csv';
'1969.csv';
'1970.csv';
'1971.csv';
'1972.csv';
'1973.csv';
'1974.csv';
'1975.csv'}
% Now make it into a table with the column named "FileNames"
t = table(FileNames)
OK, NOW we have our table and we can begin:
%==============================================
% Make a new table with the FileNames column,
% PLUS a new column called Year that is the year from the basefile name.
FileNames = t.FileNames; % Extract the first column from t
Years = zeros(length(FileNames), 1); % Preallocate array for column 2.
for row = 1 : length(FileNames)
[~, strYears, ~] = fileparts(FileNames{row}); % Get base file name.
Years(row) = str2double(strYears); % Convert from string to a number.
end
% Now we have years and we can make
% a second, output table from the two column vectors.
t2 = table(FileNames, Years)
Now you have the table you want, in an actual "table"-type variable, rather than a cell array. You could probably construct the Years column vector without a for loop but I thought that way would be easiest for you to understand and follow.
0 Comments
See Also
Categories
Find more on Matrix Indexing 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!