Import text data from Comsol
조회 수: 22 (최근 30일)
이전 댓글 표시
Hello,
I have a data (.txt file) which is exported from Comsol. Il wanna plot these data in Matlab (3d plot).
Normally, to export these data into matlab, I must do it "manually": I have to delete all string data in the .txt files: firstly the variable u (line 1-13), then the r-coordinate (line 114-115) and finally z-coordinate (line 216-217). After that, using the dlmread command to read the modified text file.
The problem is if the data length is short, it is easily to do it. But if the number of rows is 2 or many thousands lines, could we have an "automatic" method to do?
Best regards.
댓글 수: 2
Ena Ivanovic
2020년 7월 31일
Hello,
did you find a way to import the coordinates automatically into Matlab?
Thank you in advance.
Best regards
jonas
2020년 7월 31일
You could use the comsol-matlab link and just transfer any data directly to matlab.
답변 (1개)
Mario Malic
2020년 7월 31일
편집: Mario Malic
2020년 7월 31일
As recommended by Jonas, it is probably the best to use the COMSOL - MATLAB Link.
clc
clear
fclose all
Counter = 1;
FID = fopen('results.txt', 'rt');
tline = fgetl(FID);
File_Data{Counter} = tline;
while ischar(tline)
Counter = Counter+1;
tline = fgetl(FID);
File_Data{Counter} = tline;
end
fclose(FID);
File_Data = File_Data';
Wanted_Rows = 1:1:length(File_Data);
Delete_Lines = [1:13 114 115 216 217, length(File_Data)]; % Last line of File_Data is -1 which denotes end of file so we remove it here
File_Data(Delete_Lines) = [];
File_Data_Split = split(File_Data(1:length(File_Data))); % Splits the cell
Data_Ready = cellfun(@str2num,File_Data_Split);
Edit: Actually, code is okay.
Hah, I did not see that question is 4 years old. Here's an answer to it.
참고 항목
카테고리
Help Center 및 File Exchange에서 Language Support에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!