Reputation: 107
I currently have a 72 .dat files which I can upload into my matlab workspace using the folowing code;
files = dir('*.dat');
for i=1:length(files)
eval(['load ' files(i).name ' -ascii']);
end
That part works fine. Each file contains three columns of data. This first is time and the second and third are U and V velocity components. It is possible to write a code that will import all these .dat files into my workspace and then name each column of data with respect to the file name. For example each file is named Point1, Point2 etc all the way to Point73. So is it possible that the three columns in the .dat file Point1 can be named Time1, U1 and V1 and named Time2, U2 and V2 when taken from the Point2.dat file?
Currently I am trying this piece of code;
Time1 = Point1(1:1024, 1);
U1 = Point1(1:1024, 2);
V1 = Point1(1:1024, 3);
Time2 = Point2(1:1024, 1);
U2 = Point2(1:1024, 2);
V2 = Point2(1:1024, 3);
I know it is wrong but I don't know how to make the names of the variables dependent on the original file name. Any help would be greatly appreciated.
Regards, Jerry
Upvotes: 1
Views: 128
Reputation: 2256
According to Generate Field Names from Variables, you can use the bracket operator.
If you use a struct, you can do like this:
for i=1:3
varname = strcat('U',num2str(i));
a.(varname) = i;
end
a =
U1 = 1
U2 = 2
U3 = 3
Also, you should try to avoid eval
if possible, for reasons given in Alternatives to the eval Function.
You could load the variables similar to this instead:
filename = strcat('Point',num2str(i),'.dat'); % filename = Point1.dat
load(filename);
Upvotes: 1