Split excel file and get ordered splittedfile names

Dear everyone,
I have the lines as follow which splits an excel file into some excel files based on the number of Stations inside.
data = readtable('data.xlsx');
stations = unique(data.Station);
for i=1:numel(stations)
x = data(data.Station==stations(i), :);
writetable(x,"Sta_"+num2str(stations(i))+".xlsx");
end
The file I need to split is like this:
Then, my code above will generate three excel files from this excel file : Station_2.xlsx, Station_31.xlsx, Station_42.xlsx.
What I want is... Set a new file name with ordered rank : Station_1, Station_2, Station_3.
So, here I want
Station_2 ---> becomes Station_1.xlsx
Station_31 ---> becomes Station_2.xlsx
Station_42 ---> becomes Station_3.xlsx
warm regards

 採用された回答

dpb
dpb 2023 年 10 月 10 日

0 投票

Just use the looping index..
data = readtable('data.xlsx');
stations = unique(data.Station);
for i=1:numel(stations)
x = data(data.Station==stations(i), :);
writetable(x,"Station_"+ i +".xlsx");
end

3 件のコメント

Adi Purwandana
Adi Purwandana 2023 年 10 月 10 日
Thank you @dpb!
Adi Purwandana
Adi Purwandana 2023 年 10 月 10 日
Anyway, is it possible also to change the value of Station column so that following the ordered file name? I asked this in separated Ask thread indeed : question.
dpb
dpb 2023 年 10 月 10 日
data.Station=findgroups(data.Station);

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

その他の回答 (0 件)

製品

リリース

R2022a

質問済み:

2023 年 10 月 10 日

コメント済み:

dpb
2023 年 10 月 10 日

Community Treasure Hunt

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

Start Hunting!

Translated by