Changing names of excl files automatically and converting into cell array

I have a project where i must rename excel files automatically and then place each file into a cell array. i cannoyt seem to figure it out. the code i am using to try and rename the fie is not working. Here it is:
files = dir('*.xlsx');
% Loop through each
for id = 1:length(files)
% Get the file name (minus the extension)
[~, f] = fileparts(files(id).name);
% Convert to number
num = str2double(f);
if isnan(num)
% If numeric, rename
movefile(files(id).name, sprintf('%03d.xlsx', id));
end
end

1 Comment

The order of the renamed files might not be what you expect:
writematrix(1,'1.txt')
writematrix(2,'2.txt')
writematrix(10,'10.txt')
S = dir('*.txt');
S.name % note the order!
ans = '1.txt'
ans = '10.txt'
ans = '2.txt'

Sign in to comment.

 Accepted Answer

If you want to rename the file when the file name is a number only, then the condition should be -
if ~isnan(num)

6 Comments

it is not a number only sorry. They all have names like the following "160816 Subject 20 ECG"
That is not what I meant.
You have commented this -
% If numeric, rename
But for that the check you have implemented is incorrect.
Correct it by using the negation as I mentioned above.
"i cannoyt seem to figure it out. the code i am using to try and rename the fie is not working"
You have not explained what you want to achieve. That means we have to guess. Guessing is not an effective way of transferring information.
"it is not a number only sorry. They all have names like the following "160816 Subject 20 ECG""
But so far you have not told us what you want to happen (apparently broken code does not help): do you want to
  1. rename all files, regardless of filename content,
  2. rename only files that do not have solely numeric filenames,
  3. rename only files with solely numeric filenames,
  4. something else...
Dyuman Joshi guessed #3. But we don't know because you have not told us.
So what should a filename like "160816 Subject 20 ECG" be renamed as? Use words to explain.
Hi Sorry.
I have 11 files all with names to similar to "160816 Subject 20 ECG" and i would like to rename each file to 1 through 11 to make it easier for taking in the data to the cell array. Each dile is in a seperate subfolder and all each subfolder is in a larger folder like so:
I would like to rename all files, regardless of filename content. Thank you for your help
"i would like to rename each file to 1 through 11 to make it easier for taking in the data to the cell array."
Renaming the files won't make that easier. Here is an alternative:
P = 'D:/foo/bar'; % absolute or relative path to the parent directory
S = dir(fullfile(P,'Non*BP*','*S*','*Subject*ECG.xlsx'));
for k = 1:numel(S)
F = fullfile(S(k).folder,S(k).name);
T = readtable(F); % or some other suitable file importing function
S(k).data = T;
end
All of the imported data is stored in the structure S. For example, the 2nd file:
S(2).folder % filepath
S(2).name % filename
S(2).data % imported file data
If you really need the imported data in a cell array then you can simply do this:
C = {S.data};
I recommend using the structure.

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Products

Release

R2022b

Asked:

on 19 Feb 2024

Edited:

on 19 Feb 2024

Community Treasure Hunt

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

Start Hunting!