Import text data from Comsol

29 views (last 30 days)
NGUYEN  Quang Hung
NGUYEN Quang Hung on 10 Jun 2016
Commented: Ena Ivanovic on 2 Aug 2020
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 Comments
Ena Ivanovic
Ena Ivanovic on 31 Jul 2020
Hello,
did you find a way to import the coordinates automatically into Matlab?
Thank you in advance.
Best regards
jonas
jonas on 31 Jul 2020
You could use the comsol-matlab link and just transfer any data directly to matlab.

Sign in to comment.

Answers (1)

Mario Malic
Mario Malic on 31 Jul 2020
Edited: Mario Malic on 31 Jul 2020
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.

Community Treasure Hunt

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

Start Hunting!