필터 지우기
필터 지우기

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

조회 수: 1 (최근 30일)
%% 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
Stephen23
Stephen23 2023년 2월 27일
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.

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2023년 2월 27일
편집: Walter Roberson 2023년 2월 27일
[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.

카테고리

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

태그

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by