end of line

hi, what is the command that correspond end of line? I want read file and print it to array. the rows in that file are not equal in length. so, I need know what is the command deal with end of line .
thanks

답변 (4개)

Sean de Wolski
Sean de Wolski 2011년 9월 12일

1 개 추천

in textscan there is an end of line setting that gives you options:
doc textscan
Walter Roberson
Walter Roberson 2011년 9월 12일

0 개 추천

There is no specific command to write out end of line. There are multiple ways of doing this, the most straight-forward of which was already given to you when you asked this question earlier in http://www.mathworks.com/matlabcentral/answers/15614-new-line
Jan
Jan 2011년 9월 12일

0 개 추천

It is not clear, what you mean by "command". There are two characters for a line break: CHAR(13) and CHAR(10), which can be created by sprintf('\r') and sprintf('\n') also.
What do you exactly mean by "print it to an array"? Actually you can print to the command window, to a file and on paper. Most likely the question would get more clear, if you post the relevant code.

댓글 수: 3

Walter Roberson
Walter Roberson 2011년 9월 12일
The number of characters in a line break depends on the operating system and program representation. MS Windows text files use the pair of characters Jan noted; Unix-like systems use just linefeed, char(10).
Jan
Jan 2011년 9월 12일
And all Windows editors except the "Editor" accept the CHAR(10) also. E.g. M-files are written with this line break.
huda nawaf
huda nawaf 2011년 9월 12일
hi,
if I have file as follow:
ex.:1 2 3 11 0 1 2
4 5 6 2
1 0 2 4 5
in the file, the rows have not equal length .
what I want is to place this file in array as we show it
i.e three rows , the length of first row is 7, second is 4,and third is 5.
thanks in advance

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

Oleg Komarov
Oleg Komarov 2011년 9월 12일

0 개 추천

EDITED
myfile.txt contains:
1 2 3 11 0 1 2
4 5 6 2
1 0 2 4 5
% To import
fid = fopen('C:\myfile.txt')
out = textscan(fid, '%f%f%f%f%f%f%f', 'CollectOutput',1,'EmptyValue',NaN)
fclose(fid);
If your version doesn't allow 'CollectOutput', then:
out = textscan(fid, '%f%f%f%f%f%f%f','EmptyValue',NaN)
out = [out{:}]
The result:
1 2 3 11 0 1 2
4 5 6 2 NaN NaN NaN
1 0 2 4 5 NaN NaN

댓글 수: 5

huda nawaf
huda nawaf 2011년 9월 12일
is this code for all txt file.
I ran it with my txt. I got this error:
??? Error using ==> textscan
Empty Value must be a scalar double.
Error in ==> movieitem at 23
out = textscan(fid, '%f%f%f%f%f%f%f', 'CollectOutput',1,'EmptyValue','NaN')
thanks
huda nawaf
huda nawaf 2011년 9월 12일
hi,
I ran it for
1 2 3 11 0 1 2
4 5 6 2
1 0 2 4 5
also, I got the same previous error
thanks
Walter Roberson
Walter Roberson 2011년 9월 12일
Oleg had a typo, and should have used NaN instead of 'NaN'
However, EmptyValue of NaN is the default and need not be specified. It is enough to use
out = textscan(fid, '%f%f%f%f%f%f%f', 'CollectOutput',1);
and of course to access out{1} to get the array of values.
huda nawaf
huda nawaf 2011년 9월 12일
hi,I got this error when I did your suggestion:
??? Error using ==> textscan
Unknown option 'CollectOutput'.
Error in ==> movieitem at 23
out = textscan(fid, '%f%f%f%f%f%f%f', 'CollectOutput',1,'EmptyValue',NaN)
thanks
Oleg Komarov
Oleg Komarov 2011년 9월 12일
@Walter: typo corrected.
@huda: remove 'CollectOutput',1
out = textscan(fid, '%f%f%f%f%f%f%f','EmptyValue',NaN)
out = [out{:}];

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

카테고리

도움말 센터File Exchange에서 Large Files and Big Data에 대해 자세히 알아보기

태그

질문:

2011년 9월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by