How to load multiple indexed .txt tables at once using a loop?

조회 수: 2 (최근 30일)
Emerson De Souza
Emerson De Souza 2012년 1월 18일
MOTIVATION: Hi, I want to import multiple tables such as table1.txt, table2.txt, ..., tablei.txt.
WHAT I DID: For this I wrote the following lines:
for i=1:3;
x{i}=load('table%d.txt',i)';
end
PROBLEM: I obtain the following error: ??? Error using ==> load Argument must contain a string.
QUESTION How should I write the command lines to obtain
x1=load('table1.txt');
x2=load('table2.txt');
xi=load('tablei.txt');
without needing to write each line for each i?
I thank you in advance for your help
Emerson

채택된 답변

TAB
TAB 2012년 1월 18일
for i=1:3
x{i}=load(sprintf('table%d.txt',i));
end
Why you are using load() for reading text file ? You can do better with textscan().
  댓글 수: 2
Emerson De Souza
Emerson De Souza 2012년 1월 18일
Hi Tabrez,
thank you for your correction. It works well.
Now, how would I do the same using textscan()?
I wrote:
x=textscan(sprintf('table.txt'));
and obtained the message error:
??? Error using ==> textscan
Not enough input arguments.
I thank you in advance for your help
Emerson
TAB
TAB 2012년 1월 19일
You can use textscan() based on what is content of your text file and what is your expected output.
For help type
>> doc textscan
on command window
Please give some example data of your text file and your expected output, then only i can suggest anything.

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

추가 답변 (0개)

카테고리

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by