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

程序示例说明:

利用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



东西比较简单,完结;

若为所需,仅供参考。



  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值