Wei Shi
Wei Shi

Reputation: 5055

Auto generate variable name and save to file in Matlab

I have a Matlab application that generates a output matrix based on user input. I want to save it to a file or files. There are two ways I have in mind:

Multiple files; one matrix per file

user1.mat
user2.mat
....

Single file allUser.mat with multiple matrix

user1=[data1]
user1=[data2]
....

However I don't know how to keep track of the number, because every user will start a new instance of the application. Any idea how to do this?

Upvotes: 2

Views: 756

Answers (1)

eykanal
eykanal

Reputation: 27017

One way is to simply edit (or create, if it's not there already) the startup.m file to ask the user for their user id, and then use that to load the correct file:

user_id = input("Enter your user id: ");
load(sprintf('user%i.mat', user_id));

Another way would be to use the unix whoami function, if each user uses their own logon:

[s,w]=unix('whoami');  %# w = eykanal
load(sprintf('%s_data.mat', w));

Upvotes: 3

Related Questions