Reading tables from the asc files in Matlab

조회 수: 62 (최근 30일)
uzzi
uzzi 2022년 11월 9일
댓글: Mathieu NOE 2022년 11월 9일
Hello,
I have more than 400 asc files and I have to combine them all to make 4 tables in Matlab. I am searching on internet and I didn't find anyway to import the data from the asc file with the Matlab. I only saw answers as I need to convert it to .txt file for .csv files. But since I have a lot of them, can someone tell me how to solve this problem?
  댓글 수: 1
Mathieu NOE
Mathieu NOE 2022년 11월 9일
hello
what is your issue ? seems you have already accepted an answer below...

댓글을 달려면 로그인하십시오.

채택된 답변

Star Strider
Star Strider 2022년 11월 9일
편집: Star Strider 2022년 11월 9일
If they are text files, one option using readtable is in the Text Files documentation section, specifically using the name-value pair 'FileType','text'.
Example —
T = array2table(randi(9, 5, 4))
T = 5×4 table
Var1 Var2 Var3 Var4 ____ ____ ____ ____ 1 1 3 5 9 9 4 4 7 4 8 9 6 4 7 6 6 4 7 4
writetable(T, 'RandomFile.asc', 'FileType','text')
which 'RandomFile.asc'
/users/mss.system.kZx1jv/RandomFile.asc
T1 = readtable('RandomFile.asc', 'FileType','text')
T1 = 5×4 table
Var1 Var2 Var3 Var4 ____ ____ ____ ____ 1 1 3 5 9 9 4 4 7 4 8 9 6 4 7 6 6 4 7 4
.
EDIT — Corrected typographical errors.
  댓글 수: 4
Star Strider
Star Strider 2022년 11월 9일
편집: Star Strider 2022년 11월 9일
As always, my pleasure!
If they all have the same variables, you can vertically concatenate them in a loop —
Example —
for k = 1:3
T = array2table(randi(9, 5, 4))
FileName = sprintf('RandomFile%03d.asc',k)
writetable(T, FileName, 'FileType','text')
end
T = 5×4 table
Var1 Var2 Var3 Var4 ____ ____ ____ ____ 9 1 1 8 3 8 8 6 7 5 3 5 1 1 9 5 4 5 3 2
FileName = 'RandomFile001.asc'
T = 5×4 table
Var1 Var2 Var3 Var4 ____ ____ ____ ____ 4 7 6 9 8 3 8 8 8 4 6 1 2 7 4 4 3 4 3 5
FileName = 'RandomFile002.asc'
T = 5×4 table
Var1 Var2 Var3 Var4 ____ ____ ____ ____ 3 3 6 1 8 6 6 6 2 5 4 7 5 8 1 2 8 4 3 1
FileName = 'RandomFile003.asc'
Files = dir('*.asc');
for k = 1:numel(Files)
FileNames{k,:} = Files(k).name;
end
FileNames
FileNames = 3×1 cell array
{'RandomFile001.asc'} {'RandomFile002.asc'} {'RandomFile003.asc'}
for k = 1:3
Tk{k,:} = readtable(FileNames{k}, 'FileType','text');
end
Tk
Tk = 3×1 cell array
{5×4 table} {5×4 table} {5×4 table}
Tc = cat(1,Tk{:})
Tc = 15×4 table
Var1 Var2 Var3 Var4 ____ ____ ____ ____ 9 1 1 8 3 8 8 6 7 5 3 5 1 1 9 5 4 5 3 2 4 7 6 9 8 3 8 8 8 4 6 1 2 7 4 4 3 4 3 5 3 3 6 1 8 6 6 6 2 5 4 7 5 8 1 2 8 4 3 1
I never previously realised that this sort of demonstration was possible using the online Run feature!
.
uzzi
uzzi 2022년 11월 9일
This works too!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by