Load variables with same name with uigetfile and give them a new name automatically

I have different variables (cells) in my folder, that have the same name when they are loaded into the workspace with "uigetfile(...)". I would like to give each variable automatically a different name (such as var1, var2, var3...for the consecutive variables), when I load them into the workspace. I started like this but I dont get any further. Can someone help me? Thank you :)
[FileName,PathName] = uigetfile('*.mat','MultiSelect','on');
FileName=FileName';
for i=1:size(FileName,1)
var = load([PathName cell2mat(FileName(i))])
end
somehow I want to give to "var" a different name in every iteration (such as var1 in the first iteraton, var2 in the second and so on...)

1 Comment

Magically creating or accessing variable names will make your code slow, complex, buggy, and hard to debug. Read this to know why:
Instead of using bad code practices you should simply use indexing, which is simple, neat, and very efficient.

Sign in to comment.

 Accepted Answer

Don't do that!
The better approach to this problem is to use cell array or array of structs. I'd suggest array of structs for this case.
[FileName,PathName] = uigetfile('*.mat','MultiSelect','on');
for k=1:numel(FileName)
var(k).data = load(fullfile(PathName,FileName{k}));
end

8 Comments

Thank you for you quick reply. I will read the article you linked carefully soon! Your solution creates a struct array with only one field. But it should be one field per variable.
"But it should be one field per variable."
No it shouldn't. What KL gave you is a reasonably efficient solution that loads all of the data into one non-scalar structure. As you wrote in your question, the variables in the .mat files have the same names, and as such I would expect that the structure should only contain as many fields as there are variable names in the .mat files (one, apparently).
KL's solution, creating one non-scalar structure, is much simpler and more efficient than magically trying access variable names. So why do you want to change this to create lots of fields? All you need to do is to use indexing. What could be simpler?
I think his solution makes sense but i still get errors: Cell contents reference from a non-cell array object.
Cell contents reference from a non-cell array object.
Error in testdeletescript (line 3)
var(k) = load(fullfile(PathName,FileName{k}));
Are you sure you're selecting more than one file?
Anyway, you could also use a cell directly inside fullfile. Change that line to,
var(k) = load(fullfile(PathName,FileName(k)));
now all your variables inside that specific mat fille will be the fields of a struct element.
var(k) = load(fullfile(PathName,FileName{k}));
--> This creates a 1*1 struct array with only one field (it should be as many fields as files that i select)
var(k) = load(fullfile(PathName,FileName(k)));
--> This gives me following error:
Error using load
Must be a string scalar or character vector.
Thank you guys, I will continue trying!
"(it should be as many fields as files that i select)"
Using indexing is simpler. You can add the filenames if you want that too:
[FileName,PathName] = uigetfile('*.mat','MultiSelect','on');
for k=1:size(FileName,1)
var(k) = load(fullfile(PathName,FileName{k})); var(k).fname = FileName{k};
end
And then all you need is to use indexing to access them. What could be simpler?
@zeu: Ah ok. Now I understand. I forgot to change the for loop limits when I copy pasted your code. When you say size(FileName,1), it's always returning 1 (since, number of rows will always be one for that variable).
I changed it to numel (number of elemements) and I also added a field called data to the struct array var. Now it should contain data from all files. Please check the edited answer!

Sign in to comment.

More Answers (0)

Categories

Find more on App Building in Help Center and File Exchange

Asked:

zeu
on 4 Dec 2017

Commented:

zeu
on 5 Dec 2017

Community Treasure Hunt

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

Start Hunting!