Using assignin to create a dynamic name

조회 수: 21 (최근 30일)
Karina
Karina 2018년 4월 23일
편집: Stephen23 2018년 4월 23일
I have created a script which will merge multiple tables for one generator into the one. I want to assign the name of these tables based on the generator name:
files = what('MATLAB\Extract_parameter'); %list all the mat-files in the folder
month = 3; %march
m = ['0' num2str(month)];
%changing name of folder to gen index not row index
folder = dir('MATLAB\Extract_parameter');
correctnames = {folder(~[folder.isdir]).name};
tic
for i = 1:length(files.mat)
i;
GenNum = files.mat{i}; %generator mat file
tables = load(['Extract_parameter\' GenNum]); %load each file in the folder
C = struct2cell(tables);
T = vertcat(C{:}); %OUTPUT TABLE
filename = string(correctnames(i));
split = strsplit(filename,'.');
text = split(1);
name_mat = strcat(text,'_',m); %WHAT i WANT THE TABLE TO BE NAMES
name_excel = strcat(text,'_',m,'.xlsx');
assignin('base',name_mat,T);
filename_out = ['March\MatFileTables\' name_mat '.mat'];
save(filename_out,'name_mat');
%writetable(T,name_excel);
toc
end
The generators all have a unique generator number however they are not sequential, with some numbers missing when generators close down. How am I able assign the variable T to the generator name: name_mat
Currently get the error: Error using assignin Must be a string scalar or character vector.

답변 (1개)

Stephen23
Stephen23 2018년 4월 23일
편집: Stephen23 2018년 4월 23일
Using dynamic names is a red-herring, and will only cause more troubles than it solves. Read this know why:
What you want could be achieved by simply using the fieldnames of a scalar structure, and calling save with the appropriate syntax (the '-struct' option):
T = ... your table
Snew = struct(name_mat,T);
...
save(name_file,'-struct','Snew')
The option '-struct' makes save save the fields of the scalar structure Snew as individual variables in the .mat file, thus giving you exactly what you want (data saved as variables with the names of your choice), only much simpler than messing around with ugly dynamic variable names.
However I would strongly advise against doing this anyway: really you should NOT change the names of your data variables, because it makes accessing your data much more complex, hard to manage, and much buggier. Read this for a discussion of why this is the case:
Really it is much simpler to access data in a loop when the variable names are always the same, and any meta-data is stored as data in its own right, e.g. in a variable named text, or time, or date, or whatever. Looping over multiple .mat files is so much simpler if they all have the same names of variables, then you can trivially do something this:
for k = 1:numel(...)
S = load(...)
S.data
S.text
S.time
...
end
And saving them is also simpler:
for k = 1:numel(...)
data = ...
time = ...
test = ...
save(file_name,'data','time','text')
end
Note how all of the same data is included that you are trying to put into the variable names, just the code is much much simpler... which means that it is also more efficient, less buggy, and easier to debug.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by