Importing Numerically Named Excel Files into MATLAB
조회 수: 2 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
채택된 답변
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Spreadsheets에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!