MATLAB Answers

Renaming files, spectral files

17 ビュー (過去 30 日間)
Manas M
Manas M 2020 年 12 月 9 日
回答済み: Image Analyst 2020 年 12 月 9 日
I have .txt files with name" 6 splitted_0_0.txt". The 0_0 in file name denotes X and Y axes, the data is taken from a rectangular grid, so as to fetch the co-ordinates of each nodes. I wanted to rename these files so as to avoid the "6 splitted_" term. ho can i write a script for that

  0 件のコメント

サインインしてコメントする。

採用された回答

Alvery
Alvery 2020 年 12 月 9 日
Something like this might help:
function [oldFiles,newFiles] = splitFile(folder)
%SPLITFILE Finds files in folder, lists them and proposes new names
% Detailed explanation goes here
dirList = dir(folder);
isDir = [dirList(:).isdir]; % to eliminate '.', '..' and any subfolders
oldFiles = {dirList(~isDir).name};
newFiles = replace(oldFiles, 'splitted_', '');
end
then use movefile to rename the files on disk.
I'm guessing you might also like to read the files into a workspace variable, where the variable name is constructed based on the source file name. One useful concept here is the associative naming for structs.
mystruct.name = readtable('myfile.txt');
works, and so also does
mystruct.('name') = readtable('myfile.txt');
This second form is quite useful here, because you can process the filename (using functions like replace() or regexp()) and use the filename to construct a fieldname that is appropriate.

  0 件のコメント

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2020 年 12 月 9 日
Try this (untested):
folder = pwd; % Wherever you want
filePattern = fullfile(folder, '6 splitted*.txt');
dirListing = dir(filePattern); % Get a list of all files matching that wildcard pattern.
for k = 1 : length(dirListing)
% Get existing/current name.
oldName = fullfile(dirListing(k).folder, dirListing(k).name);
% Remove the string '6 splitted_' from the filename.
newName = strrep(oldName, '6 splitted_', ''); % Remove '6 splitted_' from the file name.
% Rename the file.
fprintf('Renaming\n %s\nto\n %s\n', oldName, newName);
movefile(oldName, newName);
end

  0 件のコメント

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by