readtable .txt file, reading string row issue

조회 수: 4 (최근 30일)
Birsen Ayaz-Maierhafer
Birsen Ayaz-Maierhafer 2024년 1월 25일
편집: the cyclist 2024년 1월 26일
Hi,
I have a text file with several columns and rows. The first coulmn are string while the rest of the columns are just data. I would like to pull the row named "al28" for column 4:end. But I have issue with selectiong the string value and get error.
T = readtable('Test.txt');
T2=T('al28',4:end)
Error using Test
Unrecognized table row name 'al28'.
Thank you for your help
Birsen

채택된 답변

the cyclist
the cyclist 2024년 1월 25일
편집: the cyclist 2024년 1월 26일
You need to specify that the first column is the row names:
% Specify the file name
file_path = 'Test.txt';
% Read the file into a table
T = readtable(file_path, 'Delimiter', '\t', 'ReadRowNames', true);
T2 = T('al28',4:end)
T2 = 1×3 table
Var4 Var5 Var6 ____ ________ ____ al28 48.2 4.97e-25 48.2
  댓글 수: 2
Birsen Ayaz-Maierhafer
Birsen Ayaz-Maierhafer 2024년 1월 26일
Thank you. It worked.
How can I get the list of the first columns (na24m, mg27, ...)? I would like to loop through each of them and pull the values and plot later.
I tried T3=T(:,1) but it pulled the column Var1 not the list of the elements.
Thank you for your help.
Birsen
Dyuman Joshi
Dyuman Joshi 2024년 1월 26일
편집: the cyclist 2024년 1월 26일
% Specify the file name
file_path = 'Test.txt';
% Read the file into a table
T = readtable(file_path, 'Delimiter', '\t', 'ReadRowNames', true);
T2 = T('al28',4:end)
T2 = 1×3 table
Var4 Var5 Var6 ____ ________ ____ al28 48.2 4.97e-25 48.2
"How can I get the list of the first columns (na24m, mg27, ...)?"
Using the properties of the table -
names = T.Properties.RowNames
names = 12×1 cell array
{'na24m'} {'mg27' } {'sc45m'} {'al28' } {'na24' } {'al30' } {'ne23' } {'na25' } {'cu62' } {'v52' } {'al29' } {'mn56' }
"I tried T3=T(:,1) but it pulled the column Var1 not the list of the elements."
You need to access the data stored in the table. One way is to use curly brackets.
Read more here - Access Data in Tables
T3 = T{:,1}
T3 = 12×1
0 0 0 0 0 0 0 0 0 0

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by