Problem with importdata with filename obtained two different ways

조회 수: 7 (최근 30일)
Naveen
Naveen 2012년 12월 11일
Hello,
I am trying to open a data file which contains a number in the file name. As this number keeps changing, I want to come up with the file name using string concatenation. The string concatenation works fine and I get the file name. But, when I try to load the file (tried commands load and importdata) using the obtained file name, I get the error saying "??? Error using ==> importdata at 123 Unable to open file." If I explicitly give the file name then everything works fine. I compared the file name obtained using concatenation with that of actual file name and I get '1'. I am not sure what is going wrong. Here is the code
STR1 = {'TEMP'};
STR2 = {'200'};
STR3 = {'.DAT'};
filename1 = strcat(STR1,STR2,STR3);
filename2 = 'TEMP200.DAT';
%load filename;
A = importdata(filename2);
B = importdata(filename1);
Output is here
??? Error using ==> importdata at 123
Unable to open file.
Error in ==> Averaging at 16
B = importdata(filename2);
String comparison gives '1'
TF = strcmp(filename1,filename2)
TF =
1
Any idea what is going wrong.
Thank you

답변 (2개)

Image Analyst
Image Analyst 2012년 12월 11일
편집: Image Analyst 2012년 12월 11일
I wouldn't even mess with cell arrays, but if you really want to, use sprintf() and char():
STR1 = {'TEMP'};
STR2 = {'200'};
STR3 = {'.DAT'};
filename1 = sprintf('%s%s%s', char(STR1), char(STR2), char(STR3))

Naveen
Naveen 2012년 12월 11일
Thank you for the answer. But, I can't understand how it solves my problem of opening and reading the the files TEMP200.DAT, TEMP201.DAT, TEMP202.DAT, ------, TEMP300.DAT in a for loop.

카테고리

Help CenterFile Exchange에서 Workspace Variables and MAT Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by