how to read data from file into a char array?

조회 수: 19 (최근 30일)
Ben Keshet
Ben Keshet 2020년 10월 4일
답변: Star Strider 2020년 10월 4일
Hi,
I have a file from which I want to read certain numeric data. I know the string before the data starts (say "start_string", and after it ends (say "end_string").
I have a code that kind of works, but it thorws an exception (Subscripted assignment dimension mismatch) and it looks bad.. I dont like the ~feof usage twice.
Looking for inputs on making this code better, cleaner and simpler.
Thanks!
This is my code:
i=0; % line index
fid=fopen('file_name')
while ~feof(fid)
line_tmp=fgetl(fid) % read line
if (~isempty(strfind(line_tmp,'start_string')))
while ~feof(fid)
line_copy=fgetl(fid)
if (isempty(strfind(line_tmp,'end_string')))
i=i+1;
data(i,:)=str2num(line_copy);
end
end
end
end

답변 (1개)

Star Strider
Star Strider 2020년 10월 4일
If you want to read the file as text, use the fileread function.
You can then do what you want with it.

카테고리

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

제품


릴리스

R2012a

Community Treasure Hunt

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

Start Hunting!

Translated by