I have a CSV file which I want to read in using matlab.
I've tried using csvread but it does not seem to recognize the semi-colon as the delimiter. Is there any other way?
R;W
100;0.1
200;0.5
300;0.9
You can use dlmread instead and specify the delimeter to be a semi-colon. You will also want to set the row offset to 1 so you skip the first non-numeric row.
data = dlmread('filename.csv', ';', 1);
Related
I am currently trying to read in .csv files that can have different delimiters. At the moment I am using readtable but that only handles one type and the use of textscan isn't really viable as the file contains 50+ columns.
The main two types of delimiter are ';' and ','. I am also using uigetfile in order for the user to select the file.
So I am wondering how I would go about handling more than one type of delimiter? The delimiter is consistent throughout each file and they all contain the same number of columns (hence my use of readtable).
Any suggestions would be greatly appreciated.
Thanks in advance.
I'd recommend you to unify delimiters. Eg. replace all semicolons with commas or vice versa, like that
file = 'bad_delimiters.csv';
fd = fopen(file);
text = fscanf(fd,'%c');
semicolons = strfind(text,';');
text(semicolons) = ',';
fd = fopen('good_delimiters.csv', 'w');
fwrite(fd, text);
If you have MATLAB version R2016b or later you can specify multiple delimiters for readtable by defining a set of import options, as illustrated in this example. You will first create a detectImportOptions object, change the 'Delimiter' property to a cell array of characters, then pass these options to readtable:
opts = detectImportOptions('your_file.csv');
opts.Delimiter = {';', ','};
T = readtable('your_file.csv', opts);
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
I have several tables in matlab that and I would like to write all to one .csv file, vertically concatenating. I would like to keep the column names from each table as the top row, and would like to use a loop to write the csv. The ultimate goal is to read the data in to R, but R.matlab did not work well. Suggestions about how to do this?
Alternatively how can I change filenames in a for loop using the iterator?
e.g. along the lines of
for i=1:10
writecsv('mydatai.csv',data(i))
end
So I must have at the end 10 csv files as output.
You can change the filename within the loop by using for sprintf string formatting function, for example:
dlmwrite(sprintf('mydata%i.csv', i), data(i) )
Note that the %i portion of the string is the sprintf formatting operator for an integer, it is just a coincidence that you also decided to name your iterator variable 'i'.
You can append extra data to an existing CSV by using the dlmwrite function, which uses a comma delimiter as the default, and including the '-append' flag.
Another way would be to use
writetable(Table,filename )
and to change file name after every alternation you can use
filename = ['mydata' num2str(i) '.csv']
I have a file which has a 32 line header of text, the rest is data (doubles) - one value on each line. Each line is carriage return line feed delimited. I have tried using:
fscanf
sscanf
textscan
and:
dlmread
This seemed the most likely option but I can't seem to work out how to specify a carriage return line feed as the delimiter. How do I go about doing this?
importdata is the most suitable function.
Assuming you have a file like that:
import = importdata('data.txt','',3)
data = import.data
returns:
data =
1
2
3
4
5
If you have multiple columns you can specify a delimiter:
importdata('data.txt','\t',3)
but for just one column it doesn't matter.
I have a huge csv file that I want to load with matlab. However, I'm only interested in specific columns that I know the name.
As a first step, I would like to just check how many columns the csv file has. How can I do that with matlab?
As Jonesy and erelender suggest, I would think this will do it:
fid=fopen(filename);
tline = fgetl(fid);
fclose(fid);
length(find(tline==','))+1
Since you don't seem to know what kind of carriage return character (or character encoding?) is being used then I would suggest progressively sampling your file until you encounter a recognizable CR character. One way to do this is to loop over something like
A = fscanf(fileID, ['%' num2str(N) 'c'], sizeA);
where N is the number of characters to read. At each iteration test A for presence of carriage return characters, stop if one is encountered. Once you know where the carriage return is just repeat with the right N and perform the length(find...) operation, or alternately accumulate the number of commas at each iteration. You may want to check that your file is being read along rows (is it always?), check a few samples to make sure it is.
1-) Read the first line of file
2-) Count the number of commas, or seperator characters if it is not comma
3-) Add 1 to the count and the result is the number of columns in the file.
If the csv has only numeric value you can use:
M=csvread('file_name.csv');
[row,col]=size(M);