Reading only Certain Columns from .CSV
Show older comments
I'm trying to read in the 1st and 3rd columns of data from a file.
I know this code shows the 2nd and 3rd:
M = csvread('filename.csv', 2, 2)
How do I skip the 2nd column and just display the 1st and 3rd?
Answers (1)
Walter Roberson
on 20 Mar 2019
Edited: Walter Roberson
on 20 Mar 2019
0 votes
That cannot be done with csvread() or dlmread().
With textscan() you would use a format specification of '%f %*f %f %*[^\n]' . You would probably use cell2mat() around the result of the textscan() call.
With readtable() the way to proceed would be to use detectImportOptions (new as of R2016b). You might have to give the option 'ReadVariableNames', false . Assign the result to a variable, and set the SelectedVariableNames property of the object to [1 3]. Then use readtable() on the .csv file, passing in that options object.
18 Comments
Megan Stapley
on 20 Mar 2019
Walter Roberson
on 20 Mar 2019
filename = 'filename.csv';
[fid, msg] = fopen(filename, 'rt');
if fid < 0
error('Failed to open file "%s" because "%s"', filename, msg);
end
data = cell2mat( textscan(fid, '%f,%*f,%f%*[^\n]') );
fclose(fid);
%data is now a numeric table with two columns
or
filename = 'filename.csv';
opts = detectImportOptions(filename, 'ReadVariableNames', false);
opts.SelectedVariableNames = [1 3];
data_table = readtable(filename, opts);
%data_table is now a table object with two variables.
%data_table{:,:} would be a numeric array with two columns
Megan Stapley
on 3 Apr 2019
Megan Stapley
on 3 Apr 2019
Walter Roberson
on 3 Apr 2019
Ah, ReadVariableNames was not a parameter in your R2016b release.
Please show a sample of the first 3 or 4 lines of your file.
Megan Stapley
on 3 Apr 2019
Walter Roberson
on 3 Apr 2019
Perhaps you should attach the sample as a file, so that we can check for hidden characters such as tab characters.
What you have posted cannot be read by csvread() or xlsread().
In R2017a and later, what you posted could be approached with readtable() and a FixedWidthImportOptions https://www.mathworks.com/help/matlab/ref/matlab.io.text.fixedwidthimportoptions.html . However, you only have R2016b. For your options, see the discussion at https://www.mathworks.com/matlabcentral/answers/453137-sscanf-to-extract-numbers-from-string#answer_367985
Megan Stapley
on 3 Apr 2019
Edited: Megan Stapley
on 3 Apr 2019
Walter Roberson
on 3 Apr 2019
t = readtable('BlackModelTest.csv');
times = datetime(t{:,1}/1000, 'convertfrom', 'posixtime');
elapsed = t{:,3};
My tests show that if you do
seconds(times - times(1))
then that exactly matches the elapsed data (column 3).
In order for the second column to match, we would have to assume that there is a 4 hour timezone difference
Megan Stapley
on 3 Apr 2019
Edited: Megan Stapley
on 3 Apr 2019
Walter Roberson
on 3 Apr 2019
Edited: Walter Roberson
on 3 Apr 2019
t = readtable('BlackModelTest.csv');
timestamp = datetime(t{:,1}/1000, 'convertfrom', 'posixtime');
light_sensor_reading = t{:,4};
In the sample file you provided, the second column was the text version of a timestamp, with the text being equivalent to the information that could be obtained by treating the first column as a POSIX time. There is, however, a timezone difference between the time obtained from the first column, and the text version of the time; the difference would be easiest to explain if the text version is local time in EDT (Eastern Daylight Time). If so, then you could use
t = readtable('BlackModelTest.csv');
timestamp = datetime(t{:,1}/1000, 'convertfrom', 'posixtime', 'TimeZone', 'UTC');
timestamp.TimeZone = 'America/New_York';
light_sensor_reading = t{:,4};
Megan Stapley
on 3 Apr 2019
Megan Stapley
on 3 Apr 2019
Walter Roberson
on 3 Apr 2019
Your second column is text. How do you intend to represent that in a numeric array?
What numeric value do you want stored for '2019-04-03T14.01.57.690' ?
Megan Stapley
on 3 Apr 2019
Walter Roberson
on 3 Apr 2019
perhaps
t(:,[2 4])
would work for your purposes. If not, then
[t{:.2}, num2cell(t{:,4})]
Megan Stapley
on 3 Apr 2019
Walter Roberson
on 3 Apr 2019
What would it mean to use a greater than operator with those text timestamps ?
If you want to test the sensor values you can extract them from the table:
mask = t{:,4} > 20;
which could, for example, be used in the context:
t(t{:.4}>20, [2 4])
Categories
Find more on Tables 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!