Matlab处理读取ASCII文本数据找到规则需求,重新写入数据到新的文本文件[示例:有限元边界条件施加]

xiaoxiao2021-02-28  73

程序示例说明:

利用matlab处理有限元(或光滑有限元)的边界条件施加示例,因为项目测试所需,

需要从剖分好的网格数据文件中,找到所需关系,按照规则,施加边界条件并保存为新的文件以供后续使用。

主要涉及到的有:索引规律迭代,matlab文件读写,etc.

---------------------------------------

Note:好久没写博客了,趁着一个大程序在跑,

等待结果的过程闲来无事,写个小内容如下。

---------------------------------------

直接上代码吧,代码里相应注释比较清晰:

Code 1 -------------  主程序脚本 TmpScriptforboundary.m -------------------------

%% Tmp_Apply_BoundaryConstraints %% import clear all clc struct = tmp_importfile('coord of point.out'); clear textdata %% --------simple description------------ % third edge % * * * * * * * * * * * * * * % 4th * * 2nd % edg * * edge % * * * * * * * * * * * * * * % 1st edge % #ID x y boundmarker % 1 0 0 1 4 % 2 1 0 1 % 3 2 0 1 2 % 4 2 .5 2 % 5 2 1 2 3 % 6 1 1 3 % 7 0 1 3 4 % 8 0 .5 4 % ---------------------------- %% export % ess_bound.out , the fourth boundary edge ess_val = 0; % ess_boundValue ux = ess_val*cos(rand); uy = ess_val*sin(rand); nodeid = find(data(:,2) == 0); fid = fopen('ess_bound.out', 'wt'); fprintf(fid, '#ess_bound\tID\tnodid\tux\tuy\n'); for i = 1:length(nodeid) %ess_bound(i,:) = [i nodeid(i) ux uy]; fprintf(fid, '%d\t%d\t%d\t%d\n', i, nodeid(i), ux, uy); end fclose(fid); clear ess_val ux uy nodeid fid % tract_bound.out, the second boundary edge tract_val = 1000; % tract_boundValue tx = tract_val*cos(0); ty = tract_val*sin(0); nodeid = find(data(:,2) == max(data(:,2))); fid = fopen('tract_bound.out', 'wt'); fprintf(fid, '#tract_bound\tID\tnodid\ttx\tty\n'); for i = 1:length(nodeid) %tract_bound(i,:) = [i nodeid(i) tx ty]; fprintf(fid, '%d\t%d\t%d\t%d\n', i, nodeid(i), tx, ty); end fclose(fid); clear tract_val tx ty nodeid fid %clear all

Code 2 ------------- 子函数,通过matlab导入数据自动生成的函数 tmp_importfile.m--------------

function [newData1,vars]=tmp_importfile(fileToRead1) %IMPORTFILE(FILETOREAD1) % Imports data from the specified file % FILETOREAD1: file to read % Auto-generated by MATLAB on 27-Apr-2015 15:16:10 % Import the file newData1 = importdata(fileToRead1); % Create new variables in the base workspace from those fields. vars = fieldnames(newData1); for i = 1:length(vars) assignin('base', vars{i}, newData1.(vars{i})); end

东西比较简单,完结;

若为所需,仅供参考。

转载请注明原文地址: https://www.6miu.com/read-32742.html

最新回复(0)