Extract data using textscan

2 次查看(过去 30 天)
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);

采纳的回答

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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Import and Export 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by