csvread doesn't work, I just get errors.
显示 更早的评论
I'm trying to import data from a data logger in csv format, this site says
"M = csvread('FILENAME',R,C)" is how you do that but I tried "m=csvread('LOGGER02.CSV',C5); disp(m);", but all I get is
Undefined function or variable 'C5'.
Error in untitled (line 1) m=csvread('LOGGER02.CSV',C5);
whats the real way to read csv files, assuming Matlab can read/import them? I'm using R2017b
回答(2 个)
Aarti Dwivedi
2018-7-24
R = 1
C5 = 1
m=csvread('LOGGER02.CSV',R, C5); disp(m);
The error is pretty straightforward that your variable definitions are missing. Just saying C5 doesn't mean that the cell address is C5. C5 is the name of the variable in which you will store the column offset.
19 个评论
Hank Gunderson
2018-7-24
Walter Roberson
2018-7-24
编辑:Walter Roberson
2018-7-24
csvread(filename,R1,C1) means that you want to start reading with the upper left corner skipping rows by row count given in the R1 position, and skipping columns by column count given in the C1 position. For example to have the upper left corner be row 7 column 3, then you would use csvread(filename, 6, 2) -- skipping 6 rows and 2 columns.
This does not use spreadsheet notation. The variable C1 does not mean the same as excel spreadsheet location 'C1'
If you want to specify the location 'C5' in excel spreadsheet notation, then use
csvread(filename, [], [], 'C5')
Aarti Dwivedi
2018-7-24
You are supposed to put the row number in R1, and column number in C1, R1 and C1 don't mean "row 1" or "column 1" or cell number "R1" or cell number "C1"
Hank Gunderson
2018-7-24
Walter Roberson
2018-7-24
No, csvread('LOGGER02.CSV',5) would be equivalent to csvread('LOGGER02.CSV',5,0) which would skip rows 1 through 5 and read everything below that.
If you want to read only column 5, you can use the range parameter
csvread('LOGGER02.CSV', [], [], [0 4 0 inf])
Note: what happens in these cases is that csvread() uses textscan to skip the number of rows needed by using HeaderLines, and ignores the appropriate number of leading columns in each row (finding their endpoint by ignoring their content). But everything after that is read in, and the unwanted parts are thrown away afterwards. You cannot use csvread() range parameters to avoid reading the parts of the file after a given row or column, such as for performance reasons, or in an attempt to avoid reading trailing text strings.
Hank Gunderson
2018-7-24
Walter Roberson
2018-7-24
If you are using MS Windows then
!cd C:\; dir /s LOGGER02.CSV
to locate the file.
Hank Gunderson
2018-7-24
Walter Roberson
2018-7-24
!find / -depth -type f -name LOGGER02.CSV -print
To restrict the search to your own files then
!find ~ -depth -type f -name LOGGER02.CSV -print
Reminder: Mac filesystems are permitted to be case sensitive. Depending on how you configured your individual filesystem, the above might or might not match a file named (for example) Logger02.csv
I routinely configure my Mac filesystems to be case sensitive, except for one that I reserve for use with buggy programs that have mismatches in how they talk about particular files.
Hank Gunderson
2018-7-25
编辑:Walter Roberson
2018-7-25
Hank Gunderson
2018-7-25
Walter Roberson
2018-7-25
m = csvread('LOGGER02.CSV', 0, 4, [0 4 0 inf]);
Hank Gunderson
2018-7-25
Walter Roberson
2018-7-25
m = csvread('LOGGER02.CSV', 1, 4, [1 4 inf 4]); disp(m);
Hank Gunderson
2018-7-25
Walter Roberson
2018-7-25
You know, it would be a lot easier to use readtable() especially with detectImportOptions .
Hank Gunderson
2018-7-25
Walter Roberson
2018-7-25
The simplest way is
t = readtable('LOGGER02.CSV', 'HeaderLines', 1, 'ReadVariableName', false);
temperature = t{:,2}; %or as appropriate
humidity = t{:,4}; %or as appropriate
If you have R2016b or later, you can use detectImportOptions and set the SelectedVariables property to cause it to throw away everything else when you use readtable()
For older versions of MATLAB, before R2013b, then probably it would be best to use fopen()/textscan()/fclose() . This requires knowing the format of the columns you are skipping.
Hank Gunderson
2018-7-27
Image Analyst
2018-7-24
If "C5" is the column/row you see when you open it in Excel, then row = 5 and column = 3, and csvread() expects integers for offsets, not actual row or column numbers (i.e. 4 and 2):
Starting row offset, specified as a nonnegative integer. The first row has an offset of 0.
Starting column offset, specified as a nonnegative integer. The first column has an offset of 0.
so try
m=csvread('LOGGER02.CSV', 4, 2);
类别
在 帮助中心 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!