Read dada from a file
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Adam Danz
le 20 Juil 2019
Modifié(e) : Adam Danz
le 20 Juil 2019
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.
Réponse acceptée
Adam Danz
le 20 Juil 2019
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 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Logical dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!