Saving variables in the loop.

3 Ansichten (letzte 30 Tage)
ZK
ZK am 28 Jan. 2013
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.

Akzeptierte Antwort

Azzi Abdelmalek
Azzi Abdelmalek am 28 Jan. 2013
Bearbeitet: Azzi Abdelmalek am 28 Jan. 2013
fid = fopen('file');
line1 = fgetl(fid);
res=line1;
while ischar(line1)
line1 = fgetl(fid);
res =char(res,line1)
end
fclose(fid);
  5 Kommentare
Azzi Abdelmalek
Azzi Abdelmalek am 28 Jan. 2013
Bearbeitet: Azzi Abdelmalek am 28 Jan. 2013
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 am 28 Jan. 2013
Thank You very much, working great.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Workspace Variables and MAT Files finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by