Extract data using textscan

3 visualizzazioni (ultimi 30 giorni)
Sana Ejaz
Sana Ejaz il 8 Mar 2019
Commentato: Sana Ejaz il 8 Mar 2019
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);

Risposta accettata

KSSV
KSSV il 8 Mar 2019
Read about csvread, readtable and xlsread.
[num,txt,raw] = xlsread('CRSP test.csv') ;
  3 Commenti
Stephen23
Stephen23 il 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.
Sana Ejaz
Sana Ejaz il 8 Mar 2019
Oh, I am new to this forum I didn't know that.
I appologise for that.My question haven't been resolved.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Data Import and Export in Help Center e File Exchange

Prodotti


Release

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by