So, I have an input text file(.txt) looking something like this: X:0.34, y:0.45, z:0.32, t:0.002
How can I read each variable separately like a column for x, one for y... and store them into matlab?

댓글 수: 7

Luna
Luna 2019년 7월 19일
Could you please share your .txt file to see how it is structured?
Adam Danz
Adam Danz 2019년 7월 19일
편집: Adam Danz 2019년 7월 20일
1) are the 2 colons that follow 't' intentional?
2) are the data listed in columns like this
X:0.34, y:0.45, z:0.32, t::0.002
X:0.34, y:0.45, z:0.32, t::0.002
X:0.34, y:0.45, z:0.32, t::0.002
X:0.34, y:0.45, z:0.32, t::0.002
X:0.34, y:0.45, z:0.32, t::0.002
X:0.34, y:0.45, z:0.32, t::0.002
I agree with Luna that attaching an example txt fill will save a lot of time and increase the precision to the answer that you need.
dan oltean
dan oltean 2019년 7월 20일
Adam Danz
Adam Danz 2019년 7월 20일
편집: Adam Danz 2019년 7월 20일
Volunteers shouldn't have to type that out. I'd be glad to help if there is an attached file or text that I can copy-paste that matches your inputs.
dan oltean
dan oltean 2019년 7월 20일
Sorry for the inconvenient. I attached the text here. Thank you
X:0.0511,Y:3.1803,Z:7.7935,Ts:0.0> X:-0.0519,Y:3.2234,Z:7.602,Ts:0.005004883> X:-0.2841,Y:3.2641,Z:7.5804,Ts:0.009979248> X:-0.6074,Y:3.3622,Z:7.7911,Ts:0.014953614> X:-0.8085,Y:3.458,Z:8.1622,Ts:0.019927979>
Adam Danz
Adam Danz 2019년 7월 20일
편집: Adam Danz 2019년 7월 20일
We can work with that! In the image you shared earlier, each set of x:... Y:... Z:... Ts:... has it's own row. But in the text you shared above, it looks like there can be multiple X,Y,Z,Ts per line. In fact, there are no line breaks in the text you shared so all of that is on 1 line. These details are important to set straight before designing a solution.
Tudor Oltean
Tudor Oltean 2019년 7월 20일
Each X,Y,Z,Ts has it's own row.
thank you

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

 채택된 답변

Adam Danz
Adam Danz 2019년 7월 20일

1 개 추천

Attached is the text file you described named xyz.txt.
% Read in the file
t = fileread('xyz.txt');
% Split text where '>' marks end of each row
ts = strsplit(t,'>');
% Extract X value
[Xc,~] = regexp(ts,'X:(.*),Y','tokens','match');
% Extract Y value
[Yc,~] = regexp(ts,'Y:(.*),Z','tokens','match');
% Extract Z value
[Zc,~] = regexp(ts,'Z:(.*),Ts','tokens','match');
% Extract Ts value
[Tsc,~] = regexp(ts,'Ts:(.*)$','tokens','match');
% Create table
T = table(str2double(cellfun(@(x)x{:},[Xc{:}],'UniformOutput',false))', ...
str2double(cellfun(@(x)x{:},[Yc{:}],'UniformOutput',false))', ...
str2double(cellfun(@(x)x{:},[Zc{:}],'UniformOutput',false))', ...
str2double(cellfun(@(x)x{:},[Tsc{:}],'UniformOutput',false))', ...
'VariableNames', {'X','Y','Z','Ts'});
Result
T =
5×4 table
X Y Z Ts
_______ ______ ______ _________
0.0511 3.1803 7.7935 0
-0.0519 3.2234 7.602 0.0050049
-0.2841 3.2641 7.5804 0.0099792
-0.6074 3.3622 7.7911 0.014954
-0.8085 3.458 8.1622 0.019928

댓글 수: 2

Tudor Oltean
Tudor Oltean 2019년 7월 20일
Thank you very much!!! You saved me
Have a good day! :)
Adam Danz
Adam Danz 2019년 7월 20일
편집: Adam Danz 2019년 7월 20일
Glad I could help!

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

질문:

2019년 7월 19일

편집:

2019년 7월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by