Problem with importdata with filename obtained two different ways

7 vues (au cours des 30 derniers jours)
Naveen
Naveen le 11 Déc 2012
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

Réponses (2)

Image Analyst
Image Analyst le 11 Déc 2012
Modifié(e) : Image Analyst le 11 Déc 2012
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 le 11 Déc 2012
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.

Catégories

En savoir plus sur Workspace Variables and MAT Files dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by