Load----problem in using this commad

7 次查看(过去 30 天)
i have wriiten a code to load 100 txt files of a data and the line of code marked bold is not working ....plz help me..
for i=1:1:100
tmp=num2str(i);
f={'F'};
ze={00};
tx ={'.txt'};
if(i<10)
ze={'00'};
fnam=strcat(f, ze, tmp, tx);
else
if(i==100)
fnam=strcat(f, tmp, tx);
else
ze={'0'};
fnam=strcat(f, ze, tmp, tx);
end
end
* X = load(fnam)**BOLD TEXT*
end
  2 个评论
Chandra Kurniawan
Chandra Kurniawan 2011-11-28
You can't use 'load' command to import your txt files. Load command is used to load MAT-File only. Use fopen instead.
Walter Roberson
Walter Roberson 2011-11-28
"load" _is_ supported for text files that are written to be compatible in form with save -ascii

请先登录,再进行评论。

采纳的回答

Chandra Kurniawan
Chandra Kurniawan 2011-11-28
I have 100 text file with filenames F001.txt, F002.txt, F003.txt, ... , F010.txt, F011.txt, ... F100.txt.
Use this code below to import your text files
for i = 1 : 100
if (i < 10), ze = '00';
elseif (i == 100), ze = '';
else, ze = '0';
end
filename = strcat('F',ze,num2str(i),'.txt');
fid = fopen(filename);
C(i) = textscan(fid,'%s');
end
  1 个评论
Chandra Kurniawan
Chandra Kurniawan 2011-11-28
Note that in each txt file, I wrote 00x.
Eq : I wrote 001 in F001.txt
002 in F002.txt
100 in F100.txt

请先登录,再进行评论。

更多回答(1 个)

Fangjun Jiang
Fangjun Jiang 2011-11-28
Try this first to see if it is the problem regarding your file name construction.
for i=1:100
fnam=sprintf('F%03d.txt',i)
end
  1 个评论
Walter Roberson
Walter Roberson 2011-11-28
And for those who prefer num2str to sprintf (even though num2str calls sprintf to do the work):
for i=1:100
fnam=num2str(i, 'F%03d.txt');
X=load(fnam)
end

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Get Started with MuPAD 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by