.csv file row and line

3 visualizzazioni (ultimi 30 giorni)
Srtm
Srtm il 28 Giu 2022
Risposto: Neeraj Mirji il 28 Giu 2022
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 Commenti
Chunru
Chunru il 28 Giu 2022
Can you provide a sample file?
Srtm
Srtm il 28 Giu 2022
4,15414 8,63652 0,9690033
4,17661 8,63941 2,00133
4,17657 8,6394 1,648743
5,15414 9,63652 0,2627586
5,17661 9,63941 1,408955
5,17657 9,6394 1,125135
6,15414 10,63652 -0,4156074
6,17661 10,63941 -0,2816066
6,17657 10,6394 0,1340492
7,15414 11,63652 -0,6348675
7,17661 11,63941 -1,18876

Accedi per commentare.

Risposta accettata

Neeraj Mirji
Neeraj Mirji il 28 Giu 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.

Più risposte (1)

Chunru
Chunru il 28 Giu 2022
% For earlier version of matlab
fid = fopen("test.csv")
fid = 3
If possible, use '.' as decimal point rather than ','.
% 4,15414 8,63652 0,9690033
c = textscan(fid, "%s %s %s")
c = 1×3 cell array
{11×1 cell} {11×1 cell} {11×1 cell}
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
z = 11×3
4.1541 8.6365 0.9690 4.1766 8.6394 2.0013 4.1766 8.6394 1.6487 5.1541 9.6365 0.2628 5.1766 9.6394 1.4090 5.1766 9.6394 1.1251 6.1541 10.6365 -0.4156 6.1766 10.6394 -0.2816 6.1766 10.6394 0.1340 7.1541 11.6365 -0.6349
% For later version of matlab
T = readtable("test.csv", 'DecimalSeparator', ',') % use ',' for decimal point
T = 11×3 table
Var1 Var2 Var3 ______ ______ ________ 4.1541 8.6365 0.969 4.1766 8.6394 2.0013 4.1766 8.6394 1.6487 5.1541 9.6365 0.26276 5.1766 9.6394 1.409 5.1766 9.6394 1.1251 6.1541 10.637 -0.41561 6.1766 10.639 -0.28161 6.1766 10.639 0.13405 7.1541 11.637 -0.63487 7.1766 11.639 -1.1888

Prodotti


Release

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by