0

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. Commented Nov 21, 2013 at 22:33
  • 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 :) Commented Nov 21, 2013 at 22:34

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I'm amazed by the response time here. Thanks, this seems to have done it. In the future, I'll remember not to put digits at the start!
This solution is totally correct, but there are some good reasons to avoid eval whenever possible. (mathworks.de/de/help/matlab/matlab_prog/string-evaluation.html)
1

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)

2 Comments

Note that you can get a full list of file names out of the map with with map.keys.
Never heard of the map container, but it looks great! Thank you as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.