how do i change a variable name from workspace

i have a sequence of .mat files. When i load the mat files into workspace, the variable names are composed of sing or double underscore such as 'IR0004__, IR 10001_'. Thus, i would like to change these variables to be only number. How can i delete the underscore?
Please suggest Thank you

4 Comments

"Thus, i would like to change these variables to be only number."
Valid variable names must start with a letter, they cannot be "only number".
The easiest solution is to avoid this problem entirely by load-ing into an output variable and accessing its field/s:
S = load(...)
You could then use struct2cell to trivially access the field contents, and place these into one array (numeric, cell, etc) using basic MATLAB indexing. Note that it is actually much easier to process a sequence of .mat files if they contain exactly the same variable names, because then these can be trivially imported into one non-scalar structure, and accessing using simple, efficient indexing.
Im new for matlab program, thank you for your kind suggestion. I have tried to load all mat files to a structure. As code below:
files = dir('*.mat'); for fidx = 1:numel(files) s=load(['IR',num2str(fidx,'%04d'), '.mat']) end
what should i do for the next steps?
As I wrote in my comment, to load the data into one non-scalar array all variables in the .mat files must have exactly the same variables. This appears to not be the case with your .mat files, so you cannot use this method. If the variable names were exactly the same (as they should be) then you can simply do this:
for k = 1:N
S(k) = load(...)
end
As noted, this only works if the variable names are the same! Instead, you could use a cell array:
C = cell(1,N);
for k = 1:N
S = load(...);
C(1) = struct2cell(S);
end
Note that rather than this
['IR',num2str(fidx,'%04d'), '.mat']
you should use sprintf:
sprintf('IR%04d.mat',fidx)
Dear cobeldick, I used this code from your suggestion
files = dir('*.mat'); cell(1,numel(files)); for fidx = 1:numel(files) S = load(sprintf('IRFrame%04d.mat',fidx)); C(1) = struct2cell(S); end

Sign in to comment.

 Accepted Answer

new_var_name=old_var_name;
clear('old_var_name');

4 Comments

Is it possible to rename the multiple files? For example, using a complete code to rename all the files in my workspace
yes, you can use a loop, you can proceed the following way, I am giving you an idea-
for k=1:length(old_var_name)
new_var_name(k)=old_var_name(k);
clear('old_var_name(K)');
end
Please check the answer in this link , it will get the idea.
Stephen23
Stephen23 on 24 Jul 2018
Edited: Stephen23 on 24 Jul 2018
"For example, using a complete code to rename all the files in my workspace"
Note that files are not in any MATLAB workspace, files are saved on your harddrive. Variables are in MATLAB workspace. Basic concepts, such as what variables are, are explained in the introductory tutorials:

Sign in to comment.

More Answers (1)

getname = @(x) inputname(1);
IR0004__ = 10;
str = getname(IR0004__);
newstr = erase(str,'_');
eval([newstr '=' str ';']);
clear(str);
This could help you rename one variable. But if you are looking to rename the variables containing only numbers in the name, that will not be valid variable name in any programming language.
Hope this helps!

4 Comments

Dear Kumaravelu, thank you for your kind suggestion, Due to i have tried to convert the mat file to tif, the variable names from workspace are the problem when i initial for looping. The cause is the underscore from the variable in workspace. Sorting is not consecutive. Then i tried to convert .mat to Tif with my code, the result show only 1000 Tif image, but my workspace has 10170 .mat files. If you have any idea to solve that problem, please suggest. Thank you . ( i cannot attach the figure to show you due to i was limited to upload file. If they allow me to do that, i will upload.)
1. How are you uploading the matfiles to the workspace? Using any command line or anything else? 2. How are you converting the matfiles to TIF files?
This code below was composing both uploading and converting mat to tif by using command line:
output_folder = 'D:\iSkysoft iMedia Converter Deluxe\Downloaded';
files = dir('*.MAT');
for fidx = 1:numel(files )
matcontent = load(files(fidx).name);
filenumber = regexp(files(fidx).name, '\d+', 'match', 'once');
imagename = sprintf('IR%s__', filenumber);
assert(isfield(matcontent, imagename), 'mat file %s does not contain image %s', files(fidx).name, imagename );
outputfilename = fullfile(output_folder, sprintf('test%s.tif', filenumber ));
imwrite(matcontent.(imagename), outputfilename );
end
It may help you to understand my problem because i cannot sent you a figure. the double underscore will be created when the mat files less than 10000 ( IRFrame0001__), but after 10000 the single underscore will be created (IRFrame10000_). For example, IR10007_,IR10006_,IR10007_,IR10008_,IR10009_,IR1000__,IR10010_,.. Sorting is not consecutive. Thus, i cannot use for loop to initial the variable names correctly.
Stephen23
Stephen23 on 24 Jul 2018
Edited: Stephen23 on 24 Jul 2018
"Sorting is not consecutive."
You can easily sort the filenames by downloading my FEX submission natsortfiles:
"Thus, i cannot use for loop to initial the variable names correctly."
Accessing variable names in a loop is not recommended:

Sign in to comment.

Categories

Products

Release

R2017b

Community Treasure Hunt

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

Start Hunting!