Reputation: 362
I'm importing a number of files from the local directory that end with the following characters and file extension, using
files = dir('*_eta.txt');
for file = files'
load(file.name)
end
Which produces the expected result, loading the file into the workspace.
I now wish to transform this one variable (containing two columns, 3003 rows) by splitting out the second column into another variable.
I've already have the code to do this transform, however I would like to name the new variable after the file name, this time omitting the '_eta'.
I have tried using extractBefore(file.name,'_eta')
within a who('*_eta')
loop, but find that I can't produce the results I'm after.
I appreciate dynamic variable names are not recommended within MATLAB (or any programming language for that matter) - but I'm surprised that I'm struggling so much to access a variable, cut down the variable name and perform a function.
Additionally, I'd like to keep these as arrays, rather than combining them into cells or structs.
Any help would be gratefully received!
EDIT: Motivation - this is what I'm currently doing:
t = (0:0.01:10);
load('MPConst_eta.txt'); load('MVConst_eta.txt');
load('KVPConst_eta.txt'); load('KVVConst_eta.txt');
load('SLSPConst_eta.txt'); load('SLSVConst_eta.txt');
load('MPRamp_eta.txt'); load('MVRamp_eta.txt');
load('KVPRamp_eta.txt'); load('KVVRamp_eta.txt');
load('SLSPRamp_eta.txt'); load('SLSVRamp_eta.txt');
for k = 1:3
MP_Const(:,k) = MPConst_eta((k-1)*length(t)+1:k*length(t),2);
MV_Const(:,k) = MVConst_eta((k-1)*length(t)+1:k*length(t),2);
KVP_Const(:,k) = KVPConst_eta((k-1)*length(t)+1:k*length(t),2);
KVV_Const(:,k) = KVVConst_eta((k-1)*length(t)+1:k*length(t),2);
SLSP_Const(:,k) = SLSPConst_eta((k-1)*length(t)+1:k*length(t),2);
SLSV_Const(:,k) = SLSVConst_eta((k-1)*length(t)+1:k*length(t),2);
MP_Ramp(:,k) = MPRamp_eta((k-1)*length(t)+1:k*length(t),2);
MV_Ramp(:,k) = MVRamp_eta((k-1)*length(t)+1:k*length(t),2);
KVP_Ramp(:,k) = KVPRamp_eta((k-1)*length(t)+1:k*length(t),2);
KVV_Ramp(:,k) = KVVRamp_eta((k-1)*length(t)+1:k*length(t),2);
SLSP_Ramp(:,k) = SLSPRamp_eta((k-1)*length(t)+1:k*length(t),2);
SLSV_Ramp(:,k) = SLSVRamp_eta((k-1)*length(t)+1:k*length(t),2);
end
As you can see, there's a lot of repetition of the same operation for different files - and there's going to be a LOT more, so wanted to try to do it a smart way.
Upvotes: 0
Views: 118
Reputation: 238
Try
A = load(file.name);
varname = file.name(1:end-8); % remove _eta.txt
eval([varname,'=A(:,2)']) % save the second column in the variable with name 'varname'
It's quite slow and not very elegant, but it will do the trick.
EDIT: Alternative you could use structures and save each file in one field:
files = dir('*_eta.txt');
all_data = struct;
for i = 1:numel(files)
filename = files(i).name;
dummy = load(filename)
fieldname = filename(1:end-8); % remove _eta.txt
for i = 1:3
% Use the file name to create a field of the structure and save your things in the kth column
all_data.(fieldname)(:,k) = dummy((k-1)*length(t)+1:k*length(t),2);
end
end
Of course, pre-allocating things will make things faster
Upvotes: 3