Replace a matrix on a text file

1 visualización (últimos 30 días)
Matthieu Aoun
Matthieu Aoun el 28 de En. de 2022
Comentada: Matthieu Aoun el 28 de En. de 2022
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
%%

Respuesta aceptada

Voss
Voss el 28 de En. de 2022
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.'));
Some stuff up here I want to keep because it is relevant or important or otherwise meaningful to my existence. Some stuff in here I want to replace with something better, which I hope will serve me well in the future. More stuff to keep down here.
display(char(out_data.'));
Some stuff up here I want to keep because it is relevant or important or otherwise meaningful to my existence. 10 20 30 40 50 60 More stuff to keep down here.
  1 comentario
Matthieu Aoun
Matthieu Aoun el 28 de En. de 2022
Wow thanks a lot Benjamin, it is exactly what I needed, and way beter than my pseudo solution :
fid0=fopen(file,'r');
fid=fopen('test.t19','w');
BO = [A{:}];
% RES = [B{:}];
for j=1:stressloc1-1
tline=fgetl(fid0);
% fprintf(fid,'%s\n',char(BO(j,1)));
fprintf(fid,'%s\n',tline);
end
for i=1:size(B)
fgetl(fid0);
fprintf(fid,'%13.6d%13.6d%13.6d%13.6d%13.6d\n',B(i,1),B(i,1),B(i,1),B(i,1),B(i,1));
% fprintf(fid,'%13.6E\n',B(i,1));
end
for k=stressloc2+1:size(A{1,1})
tline=fgetl(fid0);
% fprintf(fid,'%s\n',char(BO(j,1)));
fprintf(fid,'%s\n',tline);
% fprintf(fid,'%s\n',char(BO(k,1)));
end
fclose(fid)
fclose(fid0)
Considering I have 1 million+ lines, your solution is better.
Thanks again,
matt

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Large Files and Big Data en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by