Effacer les filtres
Effacer les filtres

How to import data from a .txt file that contains text and numbers

2 vues (au cours des 30 derniers jours)
Ryan Moore
Ryan Moore le 1 Nov 2019
I have a text file called D01.txt which contains the following:
%Initial Void ratio = 0.68
%Axial strain (%) Volumetric Strain (%) Sigma1' (kPa) Sigma3' (kPa)
0.013 0 155.32 140
0.033 0.015 171.55 140
I am able to import the contents of the four coloumns in MATLAB using:
D02 = load('D02.txt')
But I do not know how to automatically grab the value 0.68 at the end of the first line of the D01.txt file.
Thank you!
  1 commentaire
Katie
Katie le 1 Nov 2019
Hi, you can use textscan to pull out different data types from a text file.
There are some examples in the Matlab documentation that do what you're looking to do: https://www.mathworks.com/help/matlab/ref/textscan.html

Connectez-vous pour commenter.

Réponse acceptée

Ryan Moore
Ryan Moore le 2 Nov 2019
I did it with textscan but it's surprisingly complicated...
function [Eo] = EoGet(textfile)
C = importdata(textfile,' ',1)
chr = C{1}
Eo = cell2mat(textscan(chr,'%%Initial Void ratio = %f',1))
end

Plus de réponses (1)

Stijn Haenen
Stijn Haenen le 1 Nov 2019
Hi,
You can try this:
text=('nitial Void ratio = 0.68');
a=find(text=='=');
number=text(a+1:end);
num=str2num(number)

Catégories

En savoir plus sur Text Data Preparation dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by