Matlab Readtable Invalid parameter name: Range - matlab

I am trying to read column C from an Excel CSV file (file is too large to load entire thing). I am trying the following code:
filename='AS-1704-CT-Data-(Jan4---Jan-7)_1.csv';
T=readtable(filename, 'Delimiter', ',', 'Range', 'C:C')
The error I get says Error in (line 2), Invalid parameter name: Range.
According to the Matlab doc for readtable, Range is a valid parameter. The Name is 'Range' and the Value is 'C:C' (I've also tried 'C2:C8' while troubleshooting).
Am I missing something here?

MATLAB interprets your file as text, and according to documentation
When reading:
Text files, only these parameter names apply: FileType, ReadVariableNames, ReadRowNames, TreatAsEmpty, DatetimeType, Delimiter, HeaderLines, Format, EmptyValue, MultipleDelimsAsOne, CollectOutput, CommentStyle, ExpChars, EndOfLine, DateLocale, and Encoding.
So Range is not a valid parameter name for text files.
You could try saving your file as an excel workbook (.xls) and reading from that.

Related

Fail to Convert .mat to .csv file

I would like to convert matlab.mat file to a .csv file. Here is the loading procedure:
>> FileData = load('matlab.mat')
FileData =
struct with fields:
MIMICtable: [278340×59 table]
Then I tried to save the CSV file but the following error occurs:
>> csvwrite('file.csv',MIMICtable)
Check for missing argument or incorrect argument data type in call to function 'real'.
Error in csvwrite (line 47)
throw(e)
I am not sure the meaning of the error message. Could someone help?
Your data is a table object. Use the function writetable to write it to a CSV file. csvwrite accepts only numeric arrays as input.
Yes, this is a weird error message. On the other hand, csvwrite is no longer recommended, which means it has been superseded by a newer function, and is no longer maintained.

How to remove double quotation marks around numbers in a CSV file in Matlab

I am using csvread syntax m =csvread('reserve2.csv',7,3,[7,3,9,4]) from Matlab to read comma seperated values from a CSV file. Unfortunately the numbers in the specified rows and columns in the CSV file are listed with double quotation marks around them and I get the following error:
Error using dlmread (line 143)
Mismatch between file and format string.
Trouble reading 'Numeric' field from file (row number 1, field number 4) ==> "0","568"\n
Error in csvread (line 49)
m=dlmread(filename, ',', r, c, rng);
How can I invoke csvread such that it can read the values even when they are in double quotation marks? Or how can I write a code to get rid of the quotation marks in the CSV file?
There are several ways of reading in this file.
MATLAB's TEXTSCAN function can parse text and ignore delimiters enclosed within double quotation marks (" "). Use the TEXTSCAN function with the '%q' format type to identify strings demarcated by double quotation marks. For example:
str = 'a,A,"a,apple"';
out=textscan(str,'%s%s%q', 'delimiter',',')
This command will generate a cell array 'out' that contains 'a', 'A' and 'a,apple'.
If you are on a Windows platform and have Microsoft Excel installed, you can use the following syntax with XLSREAD to read your data into two cell arrays:
[num_data text_data] = xlsread(filename);
After executing this command, the data will be copied to 2 different arrays that treat the data differently:
"num_data" - containing numeric data only; strings and empty fields will be converted to NaN
"text_data" - containing all data read in as strings. The text between two double quotes will be parsed as a single string
Create a custom function to parse the file using multiple FREAD or FGETL commands.
For more information on these functions, refer to the documentation by executing the following at the MATLAB command prompt:
doc textscan
doc xlsread
doc fread
doc fgetl

Reading portion of CSV with multiple data types

I have a csv file with both numbers and letters that I want to read. The file also has headers(first row) but I can read them separately so that's not a concern.
What I can't solve is the fact that the file has multiple data types and that I only want to read a portion(since the file is very large), say the first 5000 rows.
I've tried xlsread with three outputs but I get the following error : "??? Error: Object returned error code: 0x800A03EC". I've also tried textscan but if I understood correctly you've to type the variable types as an input and that's not very practical for me since I have a large amount of columns. I hope this is not a duplicate but I've read other solutions and I could not apply them to my problem.
Is there a way to do this?
Thank you in advance
To test the problem i created a small test.csv file.
It contains the following lines:
header1;header2;header3
a;1;xx
b;2;yy
c;3;zz
d;4;xxx
e;5;yyy
I use the following code to read the data:
range = 'A2:C3'
[num, text, both] = xlsread('test.csv', 1, range)
Output of the both variable, that contains the text and numbers, is as expected:
both =
'a' [1] 'xx'
'b' [2] 'yy'

Reading CSV files with MATLAB?

I am trying to read in a .csv file with MATLAB. Here is my code:
csvread('out2.csv')
This is what out2.csv looks like:
03/09/2013 23:55:12,129.32,129.33
03/09/2013 23:55:52,129.32,129.33
03/09/2013 23:56:02,129.32,129.33
On windows I am able to read this exact same file with the xlsread function with no problems. I am currently on a linux machine. When I first used xlsread to read the file I was told "File is not in recognized format" so I switched to using csvread. However, using csvread, I get the following error message:
Error using dlmread (line 139)
Mismatch between file and format string.
Trouble reading number from file (row 1u, field 2u) ==> /09/2013
23:55:12,129.32,129.33\n
Error in csvread (line 48)
m=dlmread(filename, ',', r, c)
I think the '/' in the date is causing the issue. On windows, the 1st column is interpreted as a string. On linux it seems to be interpreted as a number, so it tries to read the number and fails at the backslash. This is what I think is going on at least. Any help would be really appreciated.
csvread can only read doubles, so it's choking on the date field. Use textscan.
fid = fopen('out2.csv');
out = textscan(fid,'%s%f%f','delimiter',',');
fclose(fid);
date = datevec(out{1});
col1 = out{2};
col2 = out{3};
Update (8/31/2017)
Since this was written back in 2013, MATLAB's textscan function has been updated to directly read dates and times. Now the code would look like this:
fid = fopen('out2.csv');
out = textscan(fid, '%{MM/dd/uu HH:mm:ss}D%f%f', 'delimiter', ',');
fclose(fid)
[date, col1, col2] = deal(out{:});
An alternative as mentioned by #Victor Hugo below (and currently my personal go-to for this type of situation) would be to use readtable which will accept the same formatting string as textscan but assemble the results directly into a table object:
dataTable = readtable('out2.csv', 'Format', '%{MM/dd/uu HH:mm:ss}D%f%f')
dataTable.Properties.VariableNames = {'date', 'col1', 'col2'};
dataTable =
3×3 table
date col1 col2
___________________ ______ ______
03/09/2013 23:55:12 129.32 129.33
03/09/2013 23:55:52 129.32 129.33
03/09/2013 23:56:02 129.32 129.33
Unfortunately, the documentation for csvread clearly states:
M = csvread(filename) reads a comma-separated value formatted file, filename. The file can only contain numeric values.
Since / is neither a comma, nor a numeric value, it produces an error.
You can use readtable, as it will accept any input.
https://www.mathworks.com/help/matlab/ref/readtable.html
Yeah xlsread requires Microsoft Excel to be installed, unless it is run in 'basic' mode and 'basic' mode only reads .xls .xlsx and .xlsm files.
Another alternative are a number of user-written functions posted on MATLAB's file exchange that will work on linux and are more flexible at reading non formatted content.
One example:
https://www.mathworks.com/matlabcentral/fileexchange/75219-csv2cellfast-import-csv-files-on-machines-without-excel

Matlab : Read a file name in string format from a .csv file

I am having a .csv file which contains let's say 50 rows.
At the beginning of each row I have a file name in the following format 001_02_03.bmp followed by values separated by commas. Something like this :
001_02_03.bmp,20,30,45,10,40,20
Can someone tell me how can I read the first column from the data?
I know how to obtain the data from the second column onward. I am using the csvread function like this X = csvread('filename.csv', 0, 1);. If I try to read the first column in the same manner it outputs an error, saying the csvread does not support string format.
Use textscan, ie:
fid1 = fopen(csvFileName);
X = textscan(fid1, '%s%f%f%f%f%f%f', 'Delimiter', ',');
fclose(fid1);
FirstCol = X{1, 1};
A little more detail? csvread only works with purely numeric data, so you can't use it to get in data with a .bmp, or underscores for that matter. Thus we use textscan. The funny looking format string input to textscan is just saying that the columns are, in order, of type string %s, then the next 6 columns are of type double %f%f%f%f%f%f (or you might choose to alter this to reflect an integer datatype - I personally rarely bother unless the quantity of data is huge or floating point precision is a problem).
Note, if you just wanted to get the first column and ignore the rest, you can replace the format string with %s% %*[^\n]. A final point, if your csv file has a header line, you can skip it using the HeaderLines optional input to textscan.