フィルターのクリア

Importing Numerically Named Excel Files into MATLAB

1 回表示 (過去 30 日間)
Chinwe Orie
Chinwe Orie 2018 年 8 月 8 日
編集済み: dpb 2018 年 8 月 8 日
I have some excel files called 'd1-1.csv', 'd1-2.csv', and so on until 'd1-15.csv'. However, when these files are loaded into MATLAB, MATLAB processes the 10 through 15 files before it processes the 2 through 9 files. Is there a way to change this so that MATLAB uploads them in numeric order? Thanks.

採用された回答

dpb
dpb 2018 年 8 月 8 日
編集済み: dpb 2018 年 8 月 8 日
The best solution is to not name files that way...when creating them use a format such that leading zeros are filled in the numeric fields so the lexical sort is also the numeric one...
a=1;
n=15;
for f=1:n
fn=sprintf('d%02d-%03d.csv',a,f);
csvwrite(fn,Data(:,:,f))
end
would create files
d01-001.csv
d01-002.csv
d01-003.csv
...
d01-009.csv
d01-010.csv
d01-011.csv
...
d01-015.csv
from a presumed 3D Data array by plane. Obviously use whatever data you have.
Or, read the existing files and rewrite them changing their name appropriately; that can be done in whatever order.
Or, on FEX there's a sort routine that will sort the returned names as is in natural order but I don't recall just who the submitter was at the moment.
ADDENDUM
fmti='d%dd-%d.csv'; % format to read the present file numbers
fmto='d%02dd-%03d.csv'; % format to write new file number formats
d=dir('*.csv'); % return the directory structure
for i=1:length(d) % iterate over the list
v=sscanf(d(i).name,fmti).'; % read existing numbers
fn=num2str(v,fmto); % make new filename from them
copyfile(d(i).name,fn) % copy old to new
disp(['File ' d(i).name 'copied to ' fn]) % show what did
end
Now, when certain are correct, delete the old files and then dir() will return sorted numerical order also in lexical order and "go and sin no more"... :)

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSpreadsheets についてさらに検索

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by