Save data points from a single line in a text file.

How do I pull data values from 1 line in a text file to a nx3 array?
My issue is that thousands of data points are on one single line in the text file, they're are also in the following format:
Data1 {{x1 x2 x3} {r1 r2 r3}...} Data2 {{x4 x5 x6} {r4 r5 r6}....} Data3 {{x7 x8 x9} {r7 r8 r9}....} ....
and it goes on for thousands of data points.
I want to extract and put these data points in nx3 arrays:
data1 = [x1 x2 x3; r1 r2 r3;...]
data2 = [x4 x5 x6; r4 r5 r6;...]
data3 = [x7 x8 x9; r7 r8 r9;...]
and so on...
Does this explain my need? Please help..

댓글 수: 5

Guillaume
Guillaume 2015년 11월 22일
편집: Walter Roberson 2015년 11월 23일
However you come to it (dynamically created, using switch or if), having numbered variables is a bad idea. We're going to keep on saying it because sooner or later you're going to want to operate over all these numbered variables at once and have to resort to the dreaded eval. Much better is to use a cell array / matrix.
data{i} %with i = 1:n
is always better than
data1
...
datan
In any case, in order to answer your question, we need to see a sample of your file. Particularly as you seem to have given a slightly different format in your other question (are the '{' part of the format? are the numbers integers of doubles? etc.). Please, attach a sample file to your question.
BlueSky
BlueSky 2015년 11월 22일
I see where I confused everyone, data1 was just an example I was giving.
Please see the attached text file. I want to extract all of the data that come between data_r and data_t and save them into a new variable called data_r, and I want all of the data between data_t and data_y and store them in a new var called data_t and so on.
Note I truncated my text files, there is only 6 points bet every 2 variables, but I actually have thousands between each 2 vars.
Hopefully i explained it better this time. thanks. P.S. I attached the txt file to my question above.
Do the lines of interest always have '::' in them? Or is the line of interest always line 5 and no other lines? Are there only the four variables PrepStep_fax_temp_data_r and t and y and u ?
BlueSky
BlueSky 2015년 11월 23일
Yes that is the format of the line, but it is not always line number 5. There are actually 5 variables total. I truncated it so that its not more confusing.
BlueSky
BlueSky 2015년 11월 23일
all variables are in the same format, only their names are different. Format is the same.

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

 채택된 답변

Walter Roberson
Walter Roberson 2015년 11월 22일

1 개 추천

We already described why you should not do this. See your previous question http://uk.mathworks.com/matlabcentral/answers/254991-extracting-data-from-a-text-file-into-an-array

댓글 수: 7

BlueSky
BlueSky 2015년 11월 22일
I reworded because I though I was misunderstood. I wont dynamically name these arrays, my problem is extracting these data points. Your answer did not tell me anything about extracting the data...
How are you planning to assign into the correct variable name if you are not dynamically naming the arrays? Will you be using something like
switch data_index
case 1: data1 = ....
case 2: data2 = ....
case 3: data3 = ....
...
case 3918: data3918 = ....
end
?
BlueSky
BlueSky 2015년 11월 22일
편집: BlueSky 2015년 11월 22일
I will use an if statement something like the following:
if strfind(current_line,'Data1')
data1=points;
% if the string data1 on the current line in the text file equals Data1 then name the points that I extracted earlier (with an unknown method) data1
filecontent = fileread('example.txt');
dataline = regexp(filecontent, '(?<={).*(?=})', 'match');
dataparts = regexp(dataline{1}, '::[^:]+(?=\s*)', 'match');
subvectors = regexp(dataparts, '(?<={)[^{}]+', 'match');
varvalues = cellfun(@(C) cell2mat(cellfun(@str2num, C, 'Uniform', 0).'), subvectors,'Uniform',0);
if length(varvalues) < 5; varvalues{5} = []; end
data1 = varvalues{1};
data2 = varvalues{2};
data3 = varvalues{3};
data4 = varvalues{4};
data5 = varvalues{5};
Note: this relies upon there being only one data line. Changing that would not be too rough, but you would need to decide whether the variables were assigned per data-line or instead sequentially.
Note: this relies upon '::' marking the beginning of each variable name. Changing that would not be too rough, but you would have problems under the circumstance that variable names started with a letter that could include d, D, e, or E, and the format for your numbers shifted to include scientific notation.
Note: this relies upon there being the same number of elements per row within any one given variable. It does not, however, depend upon the number of elements per row being the same for each variable and does not depend upon the number of rows being the same for each variable. In its current form it will also tolerate commas between numeric values instead of spaces.
BlueSky
BlueSky 2015년 12월 22일
Thank you much! This is great.
Can you explain what these are:
1) '(?<={).*(?=})'
2) '::[^:]+(?=\s*)'
3) '(?<={)[^{}]+'
so that I can make tweaks based on my text files...
I appreciate your help!
For you to stand a chance to tweak any of the expressions, you better learn the regular expression language.
1. means match pattern preceded by { (the (?<={)), pattern itself is as many characters as possible including none (the .*), pattern must be followed by } (the |(?=}).
2. means match two : (the ::), followed by 1 or more character that is not : (the [^:]+). The pattern must be followed by 0 or more white spaces (the (?=\s*)
3. means match pattern preceded by { (the (?<={)), pattern itself is 1 or more character than is not { or } (the [^{}]+)
BlueSky
BlueSky 2015년 12월 23일
That is extremely difficult! I will work on learning the regexp language.
Thank you for your help.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Text Data Preparation에 대해 자세히 알아보기

질문:

2015년 11월 22일

댓글:

2015년 12월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by