Import text data from Comsol

조회 수: 22 (최근 30일)
NGUYEN  Quang Hung
NGUYEN Quang Hung 2016년 6월 10일
댓글: Ena Ivanovic 2020년 8월 2일
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
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
jonas 2020년 7월 31일
You could use the comsol-matlab link and just transfer any data directly to matlab.

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

답변 (1개)

Mario Malic
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.
  댓글 수: 1
Ena Ivanovic
Ena Ivanovic 2020년 8월 2일
Thank you very much!

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

카테고리

Help CenterFile Exchange에서 Language Support에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by