Extract data using textscan
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi,
So I am using textscan for reading and storing specific columns of my raw data and below is the code I am using for it.I am trying to store data from
'new' which is a cell array to 'datasample' that is an array. when I run the code , datasample just have got first column from new and
values of first column has been copied to all other columns too.I have got 4 columns in data sample and all 4 have got values of first column of new.
Somehow 2 ,3 and 4th column of new is not there in data sample.I need all 4 columns of new into datasample.I have attached my data here.
clear
clc
nolines = 0;
fid= fopen('CRSP test.csv');
tline = fgetl(fid);
while ischar(fgetl(fid))
nolines = nolines+1;
end
fclose(fid);
fid = fopen('CRSP test.csv');
tline1 = fgetl(fid);
datasample=zeros(nolines,4);
alldata=cell(10,1);
k=1;
for i = 1:nolines
tline = fgetl(fid);
new = textscan(tline,['%f %s %*s %*s %*f %*s %f',...
repmat('%*f',[1,6]),'%f','%*[^/n]'],'delimiter',',');
datasample(i,:)= new{1,:}; %data in new is not transfering properly to datasample
if (i>1)
if (datasample(i)~=datasample(i-1))
alldata{k,1}=datasample(datasample== datasample(i-1,1));
k=k+1;
end
end
end
fclose(fid);
0 commentaires
Réponse acceptée
KSSV
le 8 Mar 2019
Read about csvread, readtable and xlsread.
[num,txt,raw] = xlsread('CRSP test.csv') ;
3 commentaires
Stephen23
le 8 Mar 2019
"I need to use textscan.Thats the only way I have to do it."
Then why did you accept this answer?
Accepting an answer tells everyone that your question has been resolved.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Data Import and Export dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!