Saving variables in the loop.

Hi Im using fgets to display every line in the file.
fid = fopen('file');
tline = fgets(fid);
while ischar(tline)
L = (tline);
tline = fgets(fid);
disp(tline)
end
I would like to save every line in the workspace in sequence as L1, L2, L3 ...
Can You propose me a solution?
Thank You.

 채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2013년 1월 28일
편집: Azzi Abdelmalek 2013년 1월 28일

0 개 추천

fid = fopen('file');
line1 = fgetl(fid);
res=line1;
while ischar(line1)
line1 = fgetl(fid);
res =char(res,line1)
end
fclose(fid);

댓글 수: 5

ZK
ZK 2013년 1월 28일
Sorry it isn't working. it returns my all file saved in workspace as res, not as every line.
Azzi Abdelmalek
Azzi Abdelmalek 2013년 1월 28일
편집: Azzi Abdelmalek 2013년 1월 28일
If you want each line, for example line 3:
line3=res(3,:)
ZK
ZK 2013년 1월 28일
Thanks, but yes I know this possibility. most important for me is have to save every line separately in workspace, becouse I work with several files and I dont know how leinght it is, and I can't appeal directly to line becouse it could not exist.
Azzi Abdelmalek
Azzi Abdelmalek 2013년 1월 28일
편집: Azzi Abdelmalek 2013년 1월 28일
fid = fopen('file');
line1 = fgetl(fid);
res=line1;
k=0
while ischar(line1)
k=k+1;
line1 = fgetl(fid);
assignin('base',sprintf('L%d',k),line1)
end
fclose(fid);
ZK
ZK 2013년 1월 28일
Thank You very much, working great.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Workspace Variables and MAT Files에 대해 자세히 알아보기

태그

질문:

ZK
2013년 1월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by