フィルターのクリア

Changing names of excl files automatically and converting into cell array

3 ビュー (過去 30 日間)
NeedHelp55
NeedHelp55 2024 年 2 月 19 日
編集済み: Stephen23 2024 年 2 月 19 日
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 件のコメント
Stephen23
Stephen23 2024 年 2 月 19 日
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'

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

採用された回答

Dyuman Joshi
Dyuman Joshi 2024 年 2 月 19 日
If you want to rename the file when the file name is a number only, then the condition should be -
if ~isnan(num)
  6 件のコメント
NeedHelp55
NeedHelp55 2024 年 2 月 19 日
I would like to rename all files, regardless of filename content. Thank you for your help
Stephen23
Stephen23 2024 年 2 月 19 日
編集済み: Stephen23 2024 年 2 月 19 日
"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.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeTiming and presenting 2D and 3D stimuli についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by