フィルターのクリア

In one loop, sort table and save each matrix in different text files, How?

1 回表示 (過去 30 日間)
Harr
Harr 2022 年 12 月 18 日
コメント済み: Voss 2022 年 12 月 18 日
Dear,
I have a big table named sampleU (a simplified table is attached!), with 5 columnes. I would like to have a loop and based on unique values from column Z create matrix for each value and save them in separate text files with different names (U1, U2, U3,....untill U= last Z).
I am aiming for example get the U1 matrix as then save it as U1.txt
U1 =
So far i manage to have the data and unique values but having trouble reading each unique value in a loop and automatecly save them in different .txt files! (The loop is not correct in the code below)
Any suggestions?
data= SampleU % reading the excel table (I have added entire table as data table/ havent read it from Excel)
Z= unique (data.Z);
for ii = 1:Z ; %% need a loop to rad all unique values in Z column
U=?? %% maybe another loop for saving each unique value as U1, U2, U3...
U'ii'=data(data.Z ==Z,:); %%Sort out the sub-matrix as an example for Z=-2 is attached
print( '.txt'); %% Save the sub-matrix as a text
end

採用された回答

Voss
Voss 2022 年 12 月 18 日
編集済み: Voss 2022 年 12 月 18 日
data = readtable('sampleU.xlsx')
data = 41×5 table
X Y Z K1 K2 ___ ___ ___ __________ __________ 15 669 -2 1.2324e-12 9.6817e-20 165 15 -7 1.7428e-12 9.6817e-20 12 154 -2 2.4647e-12 9.6817e-20 10 336 -7 3.4856e-12 9.6817e-20 150 225 -22 4.9294e-12 9.6817e-20 11 222 -27 6.9713e-12 9.6817e-20 222 989 -42 9.8589e-12 9.6817e-20 269 456 -22 1.3943e-11 9.6817e-20 369 369 -42 1.9718e-11 9.6817e-20 222 222 -1 2.7885e-11 9.6817e-20 152 152 -1 3.9435e-11 9.6817e-20 669 669 -2 5.577e-11 9.6817e-20 15 15 -7 7.8871e-11 9.6817e-20 154 154 -2 1.1154e-10 9.6817e-20 336 336 -7 1.5774e-10 9.6817e-20 225 225 -22 2.2308e-10 9.6817e-20
[uZ,~,jj] = unique(data.Z);
for ii = 1:numel(uZ)
% pick a file name for the sub-table (note that %d may not be the
% best format, particularly if data.Z has some non-integer values):
file_name = sprintf('sampleU_z=%d.txt',uZ(ii));
% write the sub-table to file:
writetable(data(jj == ii,:),file_name);
end
% show the names of the new files:
dir('*.txt')
sampleU_z=-1.txt sampleU_z=-2.txt sampleU_z=-22.txt sampleU_z=-27.txt sampleU_z=-42.txt sampleU_z=-7.txt
% check the contents of some of the resulting txt files:
t_test = readtable('sampleU_z=-1.txt')
t_test = 6×5 table
X Y Z K1 K2 ___ ___ __ __________ __________ 222 222 -1 2.7885e-11 9.6817e-20 152 152 -1 3.9435e-11 9.6817e-20 452 222 -1 1.2619e-09 9.6817e-20 365 152 -1 1.7846e-09 9.6817e-20 222 222 -1 4.0382e-08 9.6817e-20 989 152 -1 5.7109e-08 9.6817e-20
t_test = readtable('sampleU_z=-2.txt')
t_test = 7×5 table
X Y Z K1 K2 ___ ___ __ __________ __________ 15 669 -2 1.2324e-12 9.6817e-20 12 154 -2 2.4647e-12 9.6817e-20 669 669 -2 5.577e-11 9.6817e-20 154 154 -2 1.1154e-10 9.6817e-20 369 154 -2 3.5693e-09 9.6817e-20 456 669 -2 8.0764e-08 9.6817e-20 222 154 -2 1.6153e-07 9.6817e-20
  2 件のコメント
Harr
Harr 2022 年 12 月 18 日
Thank you very much! It works perfectly :)
Voss
Voss 2022 年 12 月 18 日
You're welcome!

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

その他の回答 (0 件)

カテゴリ

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

タグ

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by