I am trying to rename a matlab file using this code I was given, I having "brace indexing is not supported variables" I would replace

2 views (last 30 days)
%% Rename dSPACE Structure Filename
% Objective: To rename a recorded data file due to filename error.
% Renaming the actual filename manually will give error in parsing the data
% file. Thus, this function will help rename the filename to a new one, if
% applicable.
%
% Input: rename_file = String of the portion or all of the file name that
% you would like to change. Must be a string. Need to adjust variable
% 'idx_insert_string' for declaring the portion of the string to change.
%
% Output: MAT_rename_file = Name of new data file name.
%
% 10/8/20
[MAT_file,PathName] = uigetfile('*.mat', 'Multiselect', 'on');
% Partial change of filename at a certain point of the file
idx_insert_string = 5; % String index to cut into and insert for the file renaming of the string.
for i = 1:length(MAT_file)
load(MAT_file{i})
filename = MAT_file{i}(1:end-4);
MAT_rename_file = [rename_file filename(idx_insert_string:end)];
eval(sprintf( [MAT_rename_file '= %s'], filename));
save(MAT_rename_file, MAT_rename_file) % Save Structure Variable to the new .mat filename
end
  3 Comments
Stephen23
Stephen23 on 27 Feb 2023
It is worth commenting that your data-processing code would be simpler and more robust if the variable names inside the MAT files did not change.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 27 Feb 2023
Edited: Walter Roberson on 27 Feb 2023
[MAT_file,PathName] = uigetfile('*.mat', 'Multiselect', 'on');
When Multiselect is off then uigetfile() returns:
  • numeric 0 in both of the first two outputs if nothing was selected
  • a character vector for the basic file name (first output) and a character vector for the directory name (second output); note the directory is not certain to end in a path separator character
When Multiselect is on then uigetfile() returns:
  • numeric 0 in both of the first two outputs if nothing was selected
  • if exactly one file was selected, a character vector for the basic file name (first output) and a character vector for the directory name (second output); note the directory is not certain to end in a path separator character
  • if more than one file was selected, a cell array of character vectors for the basic file names (first output) and a character vector for the directory name (second output); note the directory is not certain to end in a path separator character
The code you are using is assuming that it is always getting a cell array of character vectors for the first output. The code needs to be improved to first test that the first output is not numeric (because the user canceled if so), and then to account for the possibility that only a single file might have been returned.
After testing to be sure the user did not cancel, then it turns out to be easy to account for the possibility that only a single file might have been returned:
MAT_file = cellstr(MAT_file);
In the case where MAT_file was already returned as a cell array of character vectors, cellstr() returns back the identical cell array. In the case where MAT_file was returned as a character vector, then cellstr() wraps the character vector into a cell array. After the call, you are certain to have a cell array.
The code should also be improved so it does not ignore the PathName after it receives it.

Categories

Find more on File Operations in Help Center and File Exchange

Tags

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!