Replace a matrix on a text file
1 回表示 (過去 30 日間)
古いコメントを表示
Hello everyone,
Few months ago I have done with the help of the forum a matlab code that extract a specific part of a 100 000+ rows text file as a matrix.
Using this matrix of 5 columns, I have created a "result" vector (called B) where values are numbers.
I would like to modify the part of the text file containing the initial matrix and replace it with my result vector which has the same number of rows but 1 column. Writematrix doesn't seem to work for specific location ?
So to be clear :
How can I replace an existing (n,5) matrix by a (n,1) at a specific location (that is known)?
Thanks for any help! If you like I can give you the file that is analysed to see how it works
Matt
Here is my code.
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%A wannabe Code to read the t19 result file from Marc/Mentat and extract stress matrix (part1)
%Then, Calculate the Dang van criterion for critical element.
% Matthieu Aoun,
% 01/22
clc
clear all
starter='=beg=52300 (Element Integration Point Values) '; %start of the matrix of interest
ender='=beg=52401 (Nodal Results) '; %end of the matrix of interest
% Material Properties
% Material_name='Aluminium Al6061';
% E=640000;
sr0=94; %tensile endurance limit released R=0
sr1=102; %tensile Endurance limit fully reversed R=-1
alpha=(3/2)*(sr1-sr0)/(2*sr0-sr1); % for tension/compression values
beta=(sr1*sr0)/(2*sr0-sr1); % for tension/compression values
% alpha = 0.232; % for bending/torsion
% beta = sr1*0.577; % for bending/torsion
xmax=beta/alpha;
try %if no error
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% matrix : Hydrostatic Stress, Von Mises, Sigma 1, Sigma 2, Sigma 3, Strain
% % column 1 2 3 4 5 6
% CREATION OF RESULT MATRIX %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
file = 'G:\\Matthieu\\Job5\\topo1\\strut2\\file9\\marc9_job1.t19';
A = textscan(fopen(file),'%s','delimiter', '\n'); %import file
stressloc1=find(strcmp(A{1,1},starter))+1 ; %location of the beginning of the matrix
stressloc2=find(strcmp(A{1,1},ender))-2 ; %location of the end of the matrix
range=stressloc2-stressloc1+1 ; %N of rows
opt = detectImportOptions(file, 'FileType','fixedwidth', 'VariableWidths',13*ones(1,6), 'ReadVariableNames',false);
opt.DataLines = [stressloc1,stressloc2];
%normal stresses and shear stress matrix
nstress = readmatrix(file,opt); nstress = nstress(:,1:5) ;
stresses(:,1)=abs((nstress(:,3)-nstress(:,5))*0.5); %tau13
stresses(:,2)=abs((nstress(:,4)-nstress(:,5))*0.5); %tau23
stresses(:,3)=abs((nstress(:,3)-nstress(:,4))*0.5); %tau12
stresses(:,4)=(nstress(:,3)+nstress(:,4)+nstress(:,5))/3; %sigmahydrosatic
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% DANG VAN CRITERION %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
maxnstress=max(stresses(:,4));
maxshear=max(stresses(:,1));
B=stresses(:,1)+alpha*stresses(:,4);
safety=beta/max(B(:,1));
min_safety_el_id = find(B==max(B(:,1)))+10;
max_stress_el_id = find(stresses(:,4)==maxnstress)+10;
result_mat(count,1)=count; %USER RESULT
result_mat(count,2)=safety;
result_mat(count,3)=min_safety_el_id;
result_mat(count,4)=max_stress_el_id;
result_mat(count,5)=maxnstress;
result_mat(count,6)=maxshear;
catch %if error
% skip this iteration
end
%%
0 件のコメント
採用された回答
Voss
2022 年 1 月 28 日
You can do this using the low-level file I/O functions fopen(), fread(), fprintf(), and fclose().
An example of how to do it:
file = 'test.txt';
file_out = 'test_output.txt';
start_str = 'existence.';
end_str = 'More';
result = (10:10:60).';
fid = fopen(file);
file_data = fread(fid).';
fclose(fid);
s_idx = strfind(file_data,start_str);
e_idx = strfind(file_data,end_str);
result_data = reshape(sprintf('%g\n',result).',1,[]);
output_data = [ ...
file_data(1:s_idx+numel(start_str)) ...
result_data ...
file_data(e_idx:end) ...
];
fid = fopen(file_out,'w');
fprintf(fid,'%s',output_data);
fclose(fid);
Showing the contents of the input and output files:
fid = fopen(file);
in_data = fread(fid);
fclose(fid);
fid = fopen(file_out);
out_data = fread(fid);
fclose(fid);
display(char(in_data.'));
display(char(out_data.'));
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Large Files and Big Data についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!