필터 지우기
필터 지우기

Help reading text file in original format

조회 수: 1 (최근 30일)
Avery Krovetz
Avery Krovetz 2016년 7월 20일
편집: Azzi Abdelmalek 2016년 7월 20일
I'm writing an assembler for a simple computer. I want to read assembler code and write it to machine code. But I'm running into problems importing data from the text file.
A sample text file looks like this:
// This file is a test file
@2
D=A; Jump
// Sample comment
@3
D=D+A
Using the lines of code:
txt = textscan(filenum,'%s');
txt = txt{1,1};
Matlab returns a 15 x 1 cell containing this:
'//'
'This'
'file'
'is'
'a'
'test'
'file'
'@2'
'D=A;'
'Jump'
'//'
'Sample'
'comment'
'@3'
'D=D+A'
I would like Matlab to read the file and generate a cell array where each cell is a new line from the original text file.

채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2016년 7월 20일
편집: Azzi Abdelmalek 2016년 7월 20일
fid=fopen('file.txt')
str=fgetl(fid);
out=[];
while ischar(str)
out{end+1,1}=str;
str=fgetl(fid);
end
fclose(fid)
out

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Import and Export에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by