필터 지우기
필터 지우기

How to get matlab import to see col with nan as number

조회 수: 2 (최근 30일)
Daniel
Daniel 2014년 12월 26일
답변: Star Strider 2014년 12월 26일
I'm using R2014a. I have a comma deliminted text file of data. The first row are headers. The first column is in a date time format which I eventually want to convert to seconds from a reference time. The rest of the file is either numbers or nan. I'm using the Matlab import routine to import the data as column vectors. If there is an nan in a column, it wants to import that col. as TEXT and creates a CELL array rather than a DOUBLE. I can manually select NUMBER from the drop down on the import screen and it seems to be fine with that, but it's a hassle to go through every column, everytime I import.
I thought I could convert the column cell arrays to numeric vectors but the mat2cell gives me an error. Error in cell2mat (line 83) m{n} = cat(1,c{:,n});
How can I get all the variables (except perhaps the date/time) into a numeric format? I've attached a truncated file as an example and a screen shot of the import.

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2014년 12월 26일
편집: Azzi Abdelmalek 2014년 12월 26일
d=importdata('import_test.csv')
h=d.textdata;
header=h(1,:)
yourdate=h(2:end,1)
YourData=d.data
%or
[a,b,c]=xlsread('import_test.csv')
m=regexp(b,',','split')
YourData=reshape([m{:}],99,[])'

추가 답변 (1개)

Star Strider
Star Strider 2014년 12월 26일
Use the textscan function:
cu = (double('C')-double('A')+1) * 26 + double('U')-double('A')+1 ;
fidi = fopen('Daniel_import_test.csv');
[d] = textscan(fidi, ['%s' repmat('%f',1,cu-1)], 'HeaderLines',1, 'Delimiter',',', 'CollectOutput',1)
chkd = d{2}(1:10, 1:10) % Peek At The Imported Data
The temporary ‘chkd’ variable lets you peek at the numeric data textscan imports. Note that it imports the date and time as a cell string (I told it to), so you can convert those later with datenum.

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by