0
votes

I'm trying to import multiple datafiles, called 1min1, 1min2, 1min3, 1min4 etc. They'll undergo some processing (subtraction of certain columns, multiplication of some values, etc) and the end result per file is one column. I want matlab to save each of these columns in a variable that is equal to the file name. So, in go multiple files 1min1, 1min2, 1min3, 1min4 etc, out come several variables with the same name, but just one column/variable.

I try to use this:

i=20; %nr of files
name='1min';   %basic filename
norm=8;      %normalisation factor

for k=1:i
    fileName = strcat(name,num2str(k));
    A= load(fileName);
    [thisshouldbethesamefilename]=(A(:,7)-A(:,6))/A(1,12)*norm;
end

In the end I want to make this a function where I only input the filename, the nr of files and the normalisation factor, and it gives me the resulting columns as separate variables. I know I could put all columns in the same file (e.g. using B(:,1), B(:,2) etc) but I want to see the original filename so I know where the data originated. I've tried a couple of things with genvarname, eval and similar things, but haven't succeeded. Probably something simple, help is appreciated!

2
I don't see why you can't do this with eval, but why do you want to? Use a cell array instead, and store the file names for reference. Possibly use containers.Map class if you want to get fancy, and allow lookups based on file name. - chappjc
I agree, it should be possible with eval, that function always confuses me a lot and I never seem to be able to get it to work :\ As for why I want it, if I used a cell array and store the file names for reference, I'd still have to look up those things. (Is that being lazy, or being impractical?) If I know I have to plot data from the 1min5 measurement, I just want to be abled to use the variable 1min5 :) - SvB

2 Answers

2
votes

Using eval should work fine. Try this:

temp =(A(:,7)-A(:,6))/A(1,12)*norm;
eval([ 'file_' name ' = temp;'])

Note that this will prepend "file_" before all the variable names. This is important for your task, because of your filename starts with a digit ("1min") it will not be recognized as a valid MATLAB variable. To put it another way, if you had just written

1min = 12

... MATLAB would throw an error.

1
votes

I would suggest to use the map container.

i=20; %nr of files
name='1min';   %basic filename
norm=8;      %normalisation factor
map = containers.Map()
for k=1:i
    fileName = strcat(name,num2str(k));
    A= load(fileName);
    map(fileName)=(A(:,7)-A(:,6))/A(1,12)*norm;
end

Reading the data is possible via

map(fileName)