필터 지우기
필터 지우기

Help with csv data preparation

조회 수: 1 (최근 30일)
FRANCISCO
FRANCISCO 2013년 11월 11일
댓글: Simon 2013년 11월 12일
I applied the code:
if true
% code
load file
fid = fopen('data.csv', 'r');
FC = textscan(fid, '%s', 'Delimiter', '\n');
fclose(fid);
FC = FC{1};
% extract data
for n = 1:length(FC)
Data{n, 1:3} = sscanf(FC{n}, '%s %d %f');
end
end
Still I have problems extracting values. Applying that CODES up before the "for" loop, matlab gives me cell:
'0000, 0,0 '
'0001, 1,0.2 '
'0010, 0,0 '
'0011, 0,0 '
'0100, 1,0.2 '
'0101, 0,0 '
'0110, 0,0 '
'0111, 0,0 '
'1000, 1,0.2 '
'1001, 0,0 '
'1010, 1,0.2 '
'1011, 0,0 ' and what I want is that I transform this into different columns as follows:
| 0 | 0 | 0 | 0 | 0 | 0 |
| 0 | 0 | 0 | 1 | 1 | 0.2 |
How could I do this?
  댓글 수: 4
Walter Roberson
Walter Roberson 2013년 11월 11일
Please show us a sample input line as known to excel, and tell us what datatype excel thinks the first column is.
FRANCISCO
FRANCISCO 2013년 11월 12일
My csv file is as follows, but also what I can generate without brackets [] of binary patterns:
Applying the above code, the imported data puts me in the first column in text format
I want every number in a column represent, for example:
[0000], 1,0.200
I represent:
| 0 | 0 | 0 | 0 | 1 | 0.20 |
Many thanks

댓글을 달려면 로그인하십시오.

채택된 답변

Simon
Simon 2013년 11월 12일
Hi!
In your cell array the second and third entry of every line are numeric values already. You may split the string in the first cell of every line like
[a, b, c, d] = strread(Data{1, 1}, '%1d%1d%1d%1d');
  댓글 수: 2
FRANCISCO
FRANCISCO 2013년 11월 12일
I run the code and it gives me error. I'm somewhat confused
Simon
Simon 2013년 11월 12일
Hi!
What code did you run? What is your input (in my case Data{1, 1})? What is the error message?
It seems you have an error converting to your Data cell array. Try
fid = fopen('data.csv', 'r');
FC = textscan(fid, '%s', 'Delimiter', '\n');
fclose(fid);
FC = FC{1};
% remove ',', replace with ' '
FC = regexprep(FC, ',', ' ');
% extract data
for n = 1:length(FC)
Data{n, 1} = sscanf(FC{n}, '%s', 1);
Data{n, 2} = sscanf(FC{n}, '%*s %d');
Data{n, 3} = sscanf(FC{n}, '%*s %*d %f');
end
Of course you will get an error if you have '[]' around your first entry in every row. You should have written that before! Remove them first.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Text Data Preparation에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by