Input only numbers from .txt file and store numbers
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi, i am trying to import ONLY numbers from a text file and then storing those numbers in seperate integers
for eg. if i write the following statement in my txt file
(X-coordinate of first element i node = 1 Y-coordinate of first element i node = 2 X-coordinate of first element j node = 3 Y-coordinate of second element j node = 4 ...)
and i want to store them in matlab like this
x1i=1
y1i=2
x1j=3
y1j=4
i have tried eveything from fscanf, fgetl, fopen. Any help or in this regard would be helpful.
1 Kommentar
Matt Gaidica
am 19 Jan. 2021
It's generally bad form to spawn new variables like that, it will be much easier to manage a matrix—is that possible for you? It sounds like you will need to do some text parsing to get the integers or a simple regex to find an integer after "=", but I would provide the actual text file for us to help.
Antworten (1)
dpb
am 19 Jan. 2021
Bearbeitet: dpb
am 19 Jan. 2021
It would be better to write a more parseable text file, at least with a delimiter if not multiple records...
With just highlevel MATLAB functions
s='your string';
a=split([s ' '],'=');
a=a(2:end);
a=extractAfter(a,' ')'
v=str2double(extractBefore(a,' '))
returns the array 1:4
Of course, regular expressions would be the other way to go.
And, as Matt says above, don't even think about poofing variables into the namespace..."there be dragons!"
0 Kommentare
Siehe auch
Kategorien
Mehr zu String Parsing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!