How to load a csv file as a datamatrix in matlab? - matlab

I try to load a csv file in matlab to use a certain column as a vector for a OLS estimation. However, my csv looks like:
Date KCFSI
13 2004-02-01 -0.67
14 2004-03-01 -0.58
15 2004-04-01 -0.57
16 2004-05-01 -0.49
17 2004-06-01 -0.67
...
and I want to have the the column KCFSI as a vector.
I tried:
x=fopen('kcfsi.csv');
kcfsi=x(:,2);
But I don't even get a matrix for my x. Just get as value : "14" for whatever reason. I want to have something like "2x100"

csvread cannot open csv files containing non-Numeric values as stated in the documentation.
The file must contain only numeric values.
So you should use textscan as explained in this answer : https://stackoverflow.com/a/19613301/11756186
Alternatively you can use the readtable built-in function
csvtable = readtable('kcfsi.csv');
kcfsi_array = csvtable.KCFSI; %Column vector with the content of the KCFSI column

fopen returns a fileID, not a matrix.
Use A = readmatrix(filename) or M = csvread(filename) instead.

Related

Read first line of CSV file that contains text in cells

I have a deco.csv file and I only want to extract B1 to K1 (20 columns of the first rows), i.e. Deco_0001 to Deco_0020.
I first make a pre-allocation:
names = string(20,1);
and what I want is when calling S(1), it gives Deco_0001; when calling S(20), it gives Deco_0020.
I have read through textscan but I do not know how to specify the range is first row and running from column 2 to column 21 of the csv file.
Also, I want save the names individually but what I have tried just save the first line in only one cell:
fid=fopen('deco.csv');
C=textscan(fid, '%s',1);
fclose(fid);
Thanks!
It's not very elegant, but this should work for you:
fid=fopen('deco.csv');
C=textscan(fid, '%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s',1,'Delimiter',',');
fclose(fid);
S = cell(20,1);
for ii = 1:20
S{ii} = C{ii+1};
end

MATLAB reading CSV file with timestamp and values

I have the following sample from a CSV file. Structure is:
Date ,Time(Hr:Min:S:mS), Value
2015:08:20,08:20:19:123 , 0.05234
2015:08:20,08:20:19:456 , 0.06234
I then would like to read this into a matrix in MATLAB.
Attempt :
Matrix = csvread('file_name.csv');
Also tried an attempt formatting the string.
fmt = %u:%u:%u %u:%u:%u:%u %f
Matrix = csvread('file_name.csv',fmt);
The problem is when the file is read the format is wrong and displays it differently.
Any help or advice given would be greatly appreciated!
EDIT
When using #Adriaan answer the result is
2015 -11 -9
8 -17 -1
So it seems that MATLAB thinks the '-' is the delimiter(separator)
Matrix = csvread('file_name.csv',1,0);
csread does not support a format specifier. Just enter the number of header rows (I took it to be one, as per example), and number of header columns, 0.
You file, however, contains non-numeric data. Thus import it with importdata:
data = importdata('file_name.csv')
This will get you a structure, data with two fields: data.data contains the numeric data, i.e. a vector containing your value. data.textdata is a cell containing the rest of the data, you need the first two column and extract the numerics from it, i.e.
for ii = 2:size(data.textdata,1)
tmp1 = data.textdata{ii,1};
Date(ii,1) = datenum(tmp1,'YYYY:MM:DD');
tmp2 = data.textdata{ii,2};
Date(ii,2) = datenum(tmp2,'HH:MM:SS:FFF');
end
Thanks to #Excaza it turns out milliseconds are supported.

Matlab to read in fix-width text file

I have a text file like below:
TestData
6.84 11.31 17.51 22.62 26.91 31.98 36.47 35.85 28.47 20.57 10.50 6.37 test1
0.24 2.62 4.94 7.17 10.39 15.37 18.73 18.29 12.26 6.46 1.15 -0.33 test2
68.47 95.04156.07218.39304.31320.22311.69269.22203.01135.60 68.18 55.09 test3
68.47 95.04156.07218.39304.31320.22311.69269.22203.01135.60 68.18 55.09 test4
...
As you can see, the first two lines are comments to ignore. In the following lines, there is a comment at the end of each line too. Each number is in the form of %6f. Also, there are blank lines in between.
I want to read in all the numbers into a matrix to make plots. I tried to use textscan, but had problems to ignore the last column, the blank lines and read in numbers that are connected (e.g., some numbers in the line: test4).
Here is the code I have by now:
data=dir('*.txt');
formatspecific='%6f%6f%6f%6f%6f%6f%6f%6f%6f%6f%6f%6f';
for i=1:length(data);
TestData1=data(i).name;
tempData=textscan(TestData1,formatspecific,'HeaderLines',2);
end
Anybody can help to make a sample code to improve the textscan part?
To use textscan to read a file, you have to "open" it before calling textscan and "close" it after; you should use
fopen to open the input file
fclose to close the input file
textscan returns a cellarray with the content read from the input file; since you are reading more than one file, you should change the way you manage the cellarray returned by textscan, actually, as it is now in your code, the data are overwritten at each iteration.
One possibility could be to store the data in an array of struct with, for example, 2 fields: the name of the input file and the data.
Another possibility could be to generate a struct whos each fields contains the data read from the input file; you can automatically generate the name of the fileds.
Another one possibility could be to store them into a a matrix.
Hereafter, you can find a script in which these three alternative have been implemented.
Code Updated (following the comment received)
In order to be able to correctly read data such as 95.04156.07 as 95.04 156.07, the format specifier should be modified from %6f to %6.2f
% Get the list of input data
data=dir('input_file*.txt');
% Define the number of data column
n_data_col=12;
% Define the number of heared lines
n_header=2;
% Build the format specifier string
% OLD format specifier
formatspecific=[repmat('%6f',1,n_data_col) '%s']
% NEW format specifier
formatspecific=[repmat('%6.2f',1,n_data_col) '%s']
% Initialize the m_data matrix (if you know in advance the numer of row of
% each input file yoiu can define since the beginning the size of the
% matrix)
m_data=[];
% Loop for input file reading
for i=1:length(data)
% Get the i-th file name
file_name=data(i).name
% Open the i-th input file
fp=fopen(file_name,'rt')
% Read the i-th input file
C=textscan(fp,formatspecific,'headerlines',n_header)
% Close the input file
fclose(fp)
% Assign the read data to the "the_data" array struct
the_data(i).f_name=file_name
the_data(i).data=[C{1:end-1}]
% Assign the data to a struct whos fileds are named after the inout file
data_struct.(file_name(1:end-4))=[C{1:end-1}]
% Assign the data to the matric "m_data
m_data=[m_data;[C{1:end-1}]]
end
Input file
TestData
6.84 11.31 17.51 22.62 26.91 31.98 36.47 35.85 28.47 20.57 10.50 6.37 test1
0.24 2.62 4.94 7.17 10.39 15.37 18.73 18.29 12.26 6.46 1.15 -0.33 test2
68.47 95.04156.07218.39304.31320.22311.69269.22203.01135.60 68.18 55.09 test3
68.47 95.04156.07218.39304.31320.22311.69269.22203.01135.60 68.18 55.09 test4
Output
m_data =
Columns 1 through 7
6.8400 11.3100 17.5100 22.6200 26.9100 31.9800 36.4700
0.2400 2.6200 4.9400 7.1700 10.3900 15.3700 18.7300
68.4700 95.0400 156.0700 218.3900 304.3100 320.2200 311.6900
68.4700 95.0400 156.0700 218.3900 304.3100 320.2200 311.6900
Columns 8 through 12
35.8500 28.4700 20.5700 10.5000 6.3700
18.2900 12.2600 6.4600 1.1500 -0.3300
269.2200 203.0100 135.6000 68.1800 55.0900
269.2200 203.0100 135.6000 68.1800 55.0900
Hope this helps.

Matlab import binary as matrix

I have a .txt file that contains a data like this:
0000000011111000
0000001110001110
0000011000011111
0001110000000001
0011000000000001
0011000000000001
0110000000000001
0100000000000001
1100000000000001
1100000000000001
1000000000000001
1100000000000010
1100000000000110
0100000000001100
0110000000011000
0011111111110000
0
//repeats like this
The 0 at the end is a label that describes the 16x16 matrix of 0's and 1's. As you can see it is actually a binary image of 0.
I need to load this file as a 16x16 matrix. I have tried importdata, textscanand fscanf but none worked for me.
The file continues in this format.
My initial tought was to use '' as a delimiter for importdata, but that did not worked.
Is there a way to achieve this?
This is one way to read the file (see here for some documentation):
fid=fopen(textfile);
dat = textscan(fid,'%s',-1); % <-- read into cell array of strings
fclose(fid);
dat=char(dat); % <-- concatenate the strings into one char array
dat = double(dat)- '0'; % <-- convert to numeric 0/1 (48 = '0'+0)
The last row will contain the number represented ("0") and superfluous stuff, you can delete with e.g. dat(end,:)=[];
Happy trails!
Edit: Although the posted answer works with the input text file and input method I used, for the OP the code requires modification (probably due to a difference in input format):
i = 1 : length (dat{1,1})
result(i,:) = double(char(dat{1,1}{i,1})) - '0';
end

stepwise regression: Undefined function ' stepwiselm' for input arguments of type 'cell'

I have one .txt file and I have converted it to first a table Ta(Ta=readtable('xxx.txt')) then an array Aa(Aa=table2array(Ta)), the .txt file contains 220 rows and 12 cols, but the table and the array only have 219 rows and 1 col. Where did I do wrong?
Then when I tried to do stepwise regression I got error message: Undefined function ' stepwiselm' for input arguments of type 'cell'.
My coad was: mdl=stepwiselm(Aa)
In the .txt file, the first raw are texts e.g. elevation, hight, yields etc. I though I could use these names to define Predictor variables and Response variable. But since these names are lost in Aa, how should I write code for stepwise regression?Thanks!
Try the following
delim = ' ';
nrhdr = 1;
A = importdata('A-100spreg2-raa06a.txt', delim, nrhdr);
A.data will be your data, A.textdata your header. A ".txt" does not contain columns, so you need to specify a delimiter (I assumed a space). You can then use your A.data in your stepwise function.
As you indicated you wanted column 10 as y, and I assume others as X, use
stepwise(A.data(:,1:9),A.data(:,10))
I wouldn't use the headers for anything other than creating labels in figures.