Read data from file

조회 수: 10 (최근 30일)
Ivan Volodin
Ivan Volodin 2018년 3월 15일
댓글: KL 2018년 3월 15일
Hello!
I was not sure which title fits better in my case so I just left it to be written in the most general way. I have the file with following structure:
Step 1
0.10190103
0.10145140
0.10097524
0.10050153
0.10003042
9.95795131E-02
9.91610140E-02
Step 2
9.81189385E-02
9.75561813E-02
9.80424136E-02
0.10000000
0.10000000
9.80617628E-02
9.77829769E-02
...
Step N
0.10000000
0.10000000
9.93788019E-02
0.11977901
0.12290157
0.12588248
0.12861508
And I need to read it from N arrays, so
V1 = [0.10190103
0.10145140
0.10097524
0.10050153
0.10003042
9.95795131E-02
9.91610140E-02]
V2 = [9.81189385E-02
9.75561813E-02
9.80424136E-02
0.10000000
0.10000000
9.80617628E-02
9.77829769E-02]
VN = [0.10000000
0.10000000
9.93788019E-02
0.11977901
0.12290157
0.12588248
0.12861508]
I have read some examples to read data into arrays, but have not got something related to my case...
Can someone help me please..?
  댓글 수: 2
KL
KL 2018년 3월 15일
Please attach your sample file using the paperclip icon.
Ivan Volodin
Ivan Volodin 2018년 3월 15일
편집: Ivan Volodin 2018년 3월 15일
Sure, please see it in attach.

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

답변 (1개)

KL
KL 2018년 3월 15일
편집: KL 2018년 3월 15일
Something quick,
d = readtable('whole res.txt');
d = table2cell(d);
d = regexprep(d,' ','');
d = cellfun(@str2double,d,'uni',0);
d(cellfun(@isnan,d)) = [];
d = reshape(cell2mat(d),12,[]);
I reckon there should be faster ways. Even using one for loop right after readtable to combine all operations inside the for-loop step should also be more efficient than this. Nevertheless, this should work.
P.S: saving multiple variables (as you mentioned in your question) is not a good idea, use a matrix instead. If some of the columns have lesser rows, use a cell array.
  댓글 수: 2
Ivan Volodin
Ivan Volodin 2018년 3월 15일
Thank you for your answer, but how it is going to work in my case if readtable fails, because "All lines of a text file must have the same number of delimiters" ..?
KL
KL 2018년 3월 15일
Where do you get these files from? A lot can be easier if the files are exported properly (with proper delimiters and using numbers instead of strings)

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

카테고리

Help CenterFile Exchange에서 Cell Arrays에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by