How to echo print variables to text files and insert string comments beside them.
13 views (last 30 days)
Show older comments
I'm having issues figuring out how to echo print data to a secondary text file to make sure that what im pulling from the main data text file is correct. This is my last step in what I need to do. My goal is to have all 4 of the attached functions print data to a text file in a way that the variable/matrix is shown and a comment can be posted beside it. I don't want each function to be writing over one another in the new text file. i.e for the extractmesh.m function I want it to echo print to the first 3 rows of the echoprint.txt then I want the extractproperties.m function to print to rows 4-7.
%Main script
clc
clear all
fid = fopen('scratch.txt');
data = textscan(fid,'%f%f%f%f','HeaderLines',0,'CollectOutput',1);
data=data{:};
file = fopen('echoprint.txt','w');
fprintf(file,'The Number of Nodes is\n')
[NumberofNodes,NumberofElements,Connectivity]=extractmesh(data);
[NodeXCoordinates,YoungModulus,Height,Width]=extractproperties(data);
[NumberofConstraints,ListofDofSpecifiedtobeZero]=extractconstraints(data);
[NumberofPointLoads,DoF,Load]=extractloads(data);
%extractmesh.m function
function [NumberofNodes, NumberofElements,Connectivity]=extractmesh(data)
firstline = data(1,:);
NumberofNodes=firstline(1);
NumberofNodes=int64(NumberofNodes);
NumberofElements=firstline(2);
NumberofElements=int64(NumberofElements);
Connectivity=data(NumberofNodes+2:NumberofNodes+1+NumberofElements,:);
Connectivity=int64(Connectivity);
end
%extractproperties.m function
function [NodeXCoordinates,YoungModulus,Height,Width]=extractproperties(data)
firstline = data(1,:);
NumberofNodes=firstline(1);
NumberofElements=firstline(2);
NodeXCoordinates=data(2:NumberofNodes+1,:);
NodeXCoordinates=int64(NodeXCoordinates);
A = data(NumberofNodes+NumberofElements+2,:);
YoungModulus = A(1);
YoungModulus=int64(YoungModulus);
Height = A(2);
Height=int64(Height);
Width = A(3);
Width=int64(Width);
end
%extractconstraints.m function
function [NumberofConstraints,ListofDofSpecifiedtobeZero]=extractconstraints(data)
firstline = data(1,:);
NumberofNodes=firstline(1);
NumberofElements=firstline(2);
NumberofConstraints=data(NumberofNodes+NumberofElements+3,:);
NumberofConstraints=int64(NumberofConstraints);
ListofDofSpecifiedtobeZero=data(NumberofNodes+NumberofElements+4,:);
ListofDofSpecifiedtobeZero=int64(ListofDofSpecifiedtobeZero);
end
%extractloads.m function
function [NumberofPointLoads,DoF,Load]=extractloads(data)
firstline = data(1,:);
NumberofNodes=firstline(1);
NumberofElements=firstline(2);
NumberofPointLoads=data(NumberofNodes+NumberofElements+5,:);
NumberofPointLoads=int64(NumberofPointLoads);
A=data(NumberofNodes+NumberofElements+6,:);
DoF=A(1);
DoF=int64(DoF);
Load=A(2);
Load=int64(Load);
end
0 Comments
Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!