Import text data from Comsol
32 次查看(过去 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
回答(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 中查找有关 String 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!