.csv file row and line
3 views (last 30 days)
Show older comments
Hi everyone;
I am using Ubuntu 18.04 and Matlab R2018a.
I have a .csv file with 50000 lines and 3 columns.
a b c
1.12 2.22 3.56
3.07 3.89 3.89
4.98 4.27 4.02
5.44 0.55 1.56
2.66 0.78 1.78
.
.
.
I need to pull values from my file as follows, how can I do it?
[a, b, c] = xlsread('/data.csv' , ......)
4 Comments
Accepted Answer
Neeraj Mirji
on 28 Jun 2022
I believe that you are trying to extract first 50,000 rows in a,b and c variable.
If data.csv file has a,b and c as column then, following code extracts first 50,000 rows in a,b and c variable from the .csv file.
dataTable = readtable('data.csv');
[a b c] = [dataTable.a dataTable.b dataTable.c];
If data.csv file has no column names then following code can be used.
dataTable = readtable('data.csv');
[a b c] = [dataTable.Var1 dataTable.Var2 dataTable.Var3];
Hope it helps.
0 Comments
More Answers (1)
Chunru
on 28 Jun 2022
% For earlier version of matlab
fid = fopen("test.csv")
If possible, use '.' as decimal point rather than ','.
% 4,15414 8,63652 0,9690033
c = textscan(fid, "%s %s %s")
n = length(c);
m = length(c{1});
x = zeros(m, n);
for i=1:m
for j=1:n
z(i, j) = str2double(strrep(c{j}{i}, ',', '.'));
end
end
z
% For later version of matlab
T = readtable("test.csv", 'DecimalSeparator', ',') % use ',' for decimal point
0 Comments
See Also
Categories
Find more on Spreadsheets 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!