0
votes

I am trying to import data from multiple numbered text files. I tried using "importdata" command with inserting variables in the file name but it did not work

here are some of my trials where I tried to open the files: C:\Users\me\Desktop\PH\1_0.1\drift C:\Users\me\Desktop\PH\1_0.2\drift . .

EQID=1;
PGA=0.1:0.1:1;
for i=1:length(PGA)
filename = sprintf('%0.0f_%0.1f',EQID,PGA(i))
S = importdata('C:\Users\me\Desktop\PH\filename\drift');
%S = importdata(sprintf('C:\Users\me\Desktop\PH\%0.0f_%0.1f\drift',EQID,PGA(i)))
end

Can you please help Thanks

1

1 Answers

1
votes

Not sure if this is the same in MATLAB, but when I try your code in Octave I get the following:

>> EQID=1;
>> PGA=0.1:0.1:1;
>> for i=1:length(PGA)
sprintf('C:\Users\me\Desktop\PH\%0.0f_%0.1f\drift',EQID,PGA(i))
end

warning: unrecognized escape sequence '\U' -- converting to 'U'
warning: unrecognized escape sequence '\m' -- converting to 'm'
warning: unrecognized escape sequence '\D' -- converting to 'D'
warning: unrecognized escape sequence '\P' -- converting to 'P'
warning: unrecognized escape sequence '\%' -- converting to '%'
warning: unrecognized escape sequence '\d' -- converting to 'd'
ans = C:UsersmeDesktopPH1_0.1drift
warning: unrecognized escape sequence '\U' -- converting to 'U'
warning: unrecognized escape sequence '\m' -- converting to 'm'
warning: unrecognized escape sequence '\D' -- converting to 'D'
warning: unrecognized escape sequence '\P' -- converting to 'P'
warning: unrecognized escape sequence '\%' -- converting to '%'
warning: unrecognized escape sequence '\d' -- converting to 'd'
ans = C:UsersmeDesktopPH1_0.2drift

etc... etc...

I would go about it the following way instead:

>> for i=1:length(PGA)
['C:\Users\me\Desktop\PH\' sprintf('%0.0f',EQID) '_' sprintf('%0.1f',PGA(i)) '\drift']
end

ans = C:\Users\me\Desktop\PH\1_0.1\drift
ans = C:\Users\me\Desktop\PH\1_0.2\drift
ans = C:\Users\me\Desktop\PH\1_0.3\drift
ans = C:\Users\me\Desktop\PH\1_0.4\drift
ans = C:\Users\me\Desktop\PH\1_0.5\drift
ans = C:\Users\me\Desktop\PH\1_0.6\drift
ans = C:\Users\me\Desktop\PH\1_0.7\drift
ans = C:\Users\me\Desktop\PH\1_0.8\drift
ans = C:\Users\me\Desktop\PH\1_0.9\drift
ans = C:\Users\me\Desktop\PH\1_1.0\drift