How to get two arrays from .txt file?

2 Ansichten (letzte 30 Tage)
Mikolaj Rebacz
Mikolaj Rebacz am 25 Jan. 2022
Bearbeitet: Stephen23 am 31 Jan. 2022
Hi, I have a problem with getting two arrays from one file. I even have a problem with getting only one, sadly.
I don't know how to skip the text and what to do with '...'. I cannot modify the file in any way.
I need to get two arrays and later use them to display some graphs.
I was looking through many threads here but no solution works.

Akzeptierte Antwort

Stephen23
Stephen23 am 25 Jan. 2022
Bearbeitet: Stephen23 am 25 Jan. 2022
MATLAB does not have ragged arrays, you could import them as vectors, e.g.:
format short G
str = fileread('arrays2.txt');
tmp = regexp(str,'=\[[^\]]+','match');
tmp = regexp(tmp,'\d+\.?\d*','match');
out = cellfun(@str2double,tmp,'uni',0)
out = 1×2 cell array
{[0 0.000424 0.000847 0.001271 0.001694 0.002118 0.002541 0.002965 0.003812 0.004659 0.005082 0.005929 0.006776 0.009318 0.010588 0.012282 0.013129 0.013976 0.0144]} {[0 1693.2 2944.7 4417 6625.5 6331.1 6478.3 6183.8 6625.5 7214.5 7435.3 7729.8 8024.3 8466 8613.2 8466 8024.3 6920 5889.4]}
out{:}
ans = 1×19
0 0.000424 0.000847 0.001271 0.001694 0.002118 0.002541 0.002965 0.003812 0.004659 0.005082 0.005929 0.006776 0.009318 0.010588 0.012282 0.013129 0.013976 0.0144
ans = 1×19
1.0e+00 * 0 1693.2 2944.7 4417 6625.5 6331.1 6478.3 6183.8 6625.5 7214.5 7435.3 7729.8 8024.3 8466 8613.2 8466 8024.3 6920 5889.4
  2 Kommentare
Mikolaj Rebacz
Mikolaj Rebacz am 25 Jan. 2022
Bearbeitet: Mikolaj Rebacz am 31 Jan. 2022
That's great and elegant solution, thank you!
Stephen23
Stephen23 am 31 Jan. 2022
Bearbeitet: Stephen23 am 31 Jan. 2022
"One question - I don't fully understand '=\[[^\]]+'. I know that we seek sentences starting with '=[' and ending with ']'. I don't know though why do we need '^', as per documentation it's supposed to seek the beginnings of sentences (and we are looking at the bracket on the end of it) and also why do we need '+' as we only have one ']' bracket at the end of each sentence?"
That regular expression matches text starting with '=[' and keeps matching any text that is not ']' (that is the actual purpose of the '^' operator), incuding all digits, spaces, newlines, etc.... anything that is not a closing square bracket, one or more times (that is the purpose of the '+' operator). That way we can get the entire content between the square brackets (if you look at its output you will also notice that it matches the opening bracket but not the closing bracket).
Broken down into its main consituent parts:
'=\[[^\]]+'
=\[ match literal text '=['
[ ] match a set of characters
^ not equal to
\] match literal text ']'
+ one or more times
For an explanation of the [] character set and the ^ syntax, see "Metacharacters" here:

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

David Hill
David Hill am 25 Jan. 2022
Why not just copy and paste?
dL1=[0 0.000424 0.000847 0.001271 0.001694...
0.002118 0.002541 0.002965 0.003812...
0.004659 0.005082 0.005929 0.006776...
0.009318 0.010588 0.012282 0.013129...
0.013976 0.0144];
F1=[0 1693.191 2944.681 4417.021 6625.532...
6331.064 6478.298 6183.83 6625.532 7214.468...
7435.319 7729.787 8024.255 8465.957...
8613.191 8465.957 8024.255 6920 5889.362];
  1 Kommentar
Mikolaj Rebacz
Mikolaj Rebacz am 25 Jan. 2022
Unfortunately I have to input it via program.
But thanks anyway

Melden Sie sich an, um zu kommentieren.


Benjamin Thompson
Benjamin Thompson am 25 Jan. 2022
textscan can handle it if you read line by line:
>> fin = fopen('arrays2.txt', 'rt')
fin =
4
>> fgets(fin)
ans =
'
'
>> fgets(fin)
ans =
'randomtext:
'
>> fgets(fin)
ans =
'
'
>> S1 = fgets(fin)
S1 =
'dL1=[0 0.000424 0.000847 0.001271 0.001694...
'
>> C = textscan(S1, 'dL1=[%f %f %f %f...')
C =
1×4 cell array
{[0]} {[4.2400e-04]} {[8.4700e-04]} {[0.0013]}
  1 Kommentar
Mikolaj Rebacz
Mikolaj Rebacz am 25 Jan. 2022
That's interesting, unfortunately it would be a big hassle to retrieve parts of the vectors and put them together. But thank you :)

Melden Sie sich an, um zu kommentieren.

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by