I have a set of files named, dipoles.0.5000.xvg , dipoles.5000.01.10000.xvg and so on. I need to read those files and save in a different data(k) matrix. In the below form I am getting an Invalid file identifier error.
clc
clear all
for i = [0 5000.01 10000.01 15000.01 20000.01 25000.01 30000.01 35000.01 40000.01 45000.01 50000.01 55000.01 60000.01 65000.01 70000.01 75000.01 80000.01 85000.01 90000.01 95000.01];
for j = 5000:5000:100000
for k = 1:1:20
Data(k) = import_xvg('dipoles.%i.%j.xvg');
end
end
end

답변 (1개)

Chunru
Chunru 2022년 7월 18일
편집: Chunru 2022년 7월 18일

0 개 추천

fn = dir("dipoles*.*");
for i = 1:length(fn)
x{i} = readmatrix(fn(i).name, "NumHeaderLines", 27);
end
x
x = 1×2 cell array
{12×5 double} {18×5 double}

댓글 수: 5

Avik Mahata
Avik Mahata 2022년 7월 18일
Thanks for your response. The Data is a matrix, and its needs to save in the Data{i} for each import_xvg operation. I am getting an error "Unable to perform assignment because brace indexing is not supported for variables of this type". How can save them in a Data1, Data2... Data100 variable then stack them together in a bigger Data matrix like, Data = [Data1
Data2
......
Data100]
Thanks
Chunru
Chunru 2022년 7월 18일
편집: Chunru 2022년 7월 18일
Can you attach 2 or 3 sample files and import_xvg program?
Or you need to check what import_xvg returns by looking into it documentation.
Avik Mahata
Avik Mahata 2022년 7월 18일
편집: Avik Mahata 2022년 7월 18일
I had to rename the files to .txt. I don't really have to use import_xvg, I can use simple dlmread/readmatrix etc. and ignore the first few lines of text. That will probably be lot faster as well. I tried that but somehow its not reading the matrix out the file properly. All I have to do is take those matrices out of the files and put in a master file in order.
Chunru
Chunru 2022년 7월 18일
See above.
Avik Mahata
Avik Mahata 2022년 7월 18일
Thanks. It works.

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

카테고리

도움말 센터File Exchange에서 Workspace Variables and MAT Files에 대해 자세히 알아보기

질문:

2022년 7월 18일

댓글:

2022년 7월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by