How to split a cell array by format (into columns) using fileread command

조회 수: 1 (최근 30일)
Dave
Dave 2016년 8월 5일
댓글: Dave 2016년 8월 5일
I have a text file with text and numbers. there is no constant delimiter. Need to import the file into matrix. I have it done with textscan command, but it must be done with fileread.
With textscan:
fid = fopen( 'file.crn.txt' );
textformat = ['%6c%4c',repmat('%4c%3c',1,10)];
cell_array = textscan( fid, textformat,'headerLines', 3, 'Whitespace', '');
fclose( fid );
string_to_number = cellfun( @(str) str2num( str ), cell_array, 'uni', false );
Matrix=cell2mat( string_to_number );
with fileread got it into cells, but can't split them to the same format as in textscan. Any ideas?
run1 = fileread('file.crn.txt');
run2 = strsplit(run1,'\n');
run3 = run2.';
Thank you!

답변 (1개)

Shameer Parmar
Shameer Parmar 2016년 8월 5일
Try with this command
Data = textread('abc.txt', '%s', 'delimiter', '')
Here.. "Data" will be the new cell variable, which contains whole data of your text file.
then you need to apply another for loop to check the data line by line and catch the data whatever you want by applying such condition..OR by converting string into num value..
for i = 1:length(Data)
% Your Logic Here..
end
  댓글 수: 1
Dave
Dave 2016년 8월 5일
Thaks for the answer! but my file does not contain a delimiter unfortunately.

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

카테고리

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