1

I want to make a script that saves multiple base variables from the "base name". My attempt looks like this :

function [outargs] = save_with_basename(b)

basePath = 'C:\path\';

Var1 = evalin('base', [b '_1']);
Var2 = evalin('base', [b '_2']); % .. etc

for i=1:N
    save([basePath b '_' int2str(i) '.mat'], ['Var' int2str(i)]);
end

end

This saves the files but the variable saved in the file is called Var1 (see the pic.), but I want it to be called 'Foo_1' if the function was called with :

save_with_basename('Foo');

I think the second argument to save works with the function variable, so it looks like I have to change its name dynamically (which is probably not possible?) , so I wonder if there is a way I can do it.

Here is the problem : enter image description here

Thanks for any help !

1 Answer 1

4

For the sake of everything that is holy, please do not do this.

If you really have to, please please please do not do this.

If you really really really have to want to, you indeed need to use dynamic variables (but that doesn't make much of a difference now, does it?):

for i=1:N %what's N again?
   evalin('base',['Var' num2str(i) '=' b '_' num2str(i)]);
   evalin('base',['save([''' basePath b '_' num2str(i) '.mat''], [''Var' num2str(i) '''])']);
end

This will essentially perform

Var1 = "b"_1; %with whatever b is
Var2 = "b"_2;
...
save(['C:\path\b_1.mat'],['Var1']); %with whatever b is
save(['C:\path\b_2.mat'],['Var2']);

in your base workspace, so it will generate the Var* variables there. Small price to pay for selling your soul to the devil. Note that I may have missed the escaping of the single quotes in the second evalin.

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

8 Comments

Thanks for your answer Andras. Yes this is probably not the best way to do what I'm doing. But I remember I wanted to do this before, too. Just wondered if there is a simple solution. Thanks again.
@halilpazarlama I would suggest avoiding the generation of b_1,b_2,... in the first place: use cells, structs or arrays depending on your data. Then you can pass the whole data set to your function for export. Hint: you can access varname.fieldname like this: fieldstr='fieldname'; varname.(fieldstr).
Keeping them as fields or cell contents would probably be better, yes. But in my case there are few (N=3) but each one contains big matrices. And I'm working on a PC with a limited memory, (also have to share them with Dropbox) So I want to save them inside separate files, in case I want to use only one of them.
@halilpazarlama, all I'm saying is that instead of b1,b2,b3, have b{1},b{2},b{3}; then you can pass b, the cell, to your exporting function, which can then do Var1=b{1}; Var2=b{2}; Var3=b{3}; save(...,Var1); and so on. Worst case scenario: you need a single eval for the generation of the var name Var1. Or you could use structs, like b.f1, b.f2, b.f3, after which for i=1:N, Var.(['f' num2str(i)])=b.(['f' num2str(i)]); end, and --- tadaa --- no more evals;)
I think I understand now. Ok I will try like this. Thanks :)
|

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.