Negative number using 'csvread' ?

Hi, i'd like to know if there is a chance to read a negative number using 'csvread()' command?
file:
1 -2 10
3 4 -8
Using 'csvread()' I get an array:
1 2 10
3 4 8
Is there any solution? I need to get those negative numbers.

답변 (1개)

Walter Roberson
Walter Roberson 2015년 6월 9일

1 개 추천

When I use csvread() on a file containing that text, I only get back the first line. But it does have negative values.
When I use dlmread() on the file then everything comes out properly.
Just in case there is something odd about the file you are working with, could you attach a copy of the actual file?

댓글 수: 4

K T
K T 2015년 6월 9일
편집: K T 2015년 6월 9일
My bad! I'm sorry. Of course the file I am working with has a comma-separated values. I used dlmread() but it worked exacly like csvread().
EDIT: Is it possible that it happens because there is a value '-0' ? In fact this is '-0,14' (e.g.) so i really need to know if there is a minus before that zero.
Walter Roberson
Walter Roberson 2015년 6월 9일
You are trying to use comma for decimal point indicator and for separating values. csvread() and dlmread() are not suitable for reading that. textscan() is not either, not in direct form. To process it you need to convert the commas into decimal points.
I will put some code together in a moment.
fid = fopen('probav2.csv', 'rt');
C = textscan(fid, repmat('%s',1,4*2), 'Delimiter',',');
fclose(fid);
for K = 1 : 4
cols{K} = str2double(strcat(C{K*2-1},{'.'},C{K*2}));
end
now cols{1} will be the first column, cols{2} the second, and so on. If you want them all in one matrix,
Data = horzcat(cols{:});
K T
K T 2015년 6월 9일
It works! Thank you so much.

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

카테고리

질문:

K T
2015년 6월 9일

댓글:

K T
2015년 6월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by