How to get two arrays from .txt file?

조회 수: 1 (최근 30일)
Mikolaj Rebacz
Mikolaj Rebacz 2022년 1월 25일
편집: Stephen23 2022년 1월 31일
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.

채택된 답변

Stephen23
Stephen23 2022년 1월 25일
편집: Stephen23 2022년 1월 25일
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
Mikolaj Rebacz
Mikolaj Rebacz 2022년 1월 25일
편집: Mikolaj Rebacz 2022년 1월 31일
That's great and elegant solution, thank you!
Stephen23
Stephen23 2022년 1월 31일
편집: Stephen23 2022년 1월 31일
"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:

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

추가 답변 (2개)

David Hill
David Hill 2022년 1월 25일
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
Mikolaj Rebacz
Mikolaj Rebacz 2022년 1월 25일
Unfortunately I have to input it via program.
But thanks anyway

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


Benjamin Thompson
Benjamin Thompson 2022년 1월 25일
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
Mikolaj Rebacz
Mikolaj Rebacz 2022년 1월 25일
That's interesting, unfortunately it would be a big hassle to retrieve parts of the vectors and put them together. But thank you :)

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

카테고리

Help CenterFile Exchange에서 Text Data Preparation에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by