How to format my data in a text file so I can use fopen?
7 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I am trying to format the data in this excel file into a text file so that I can use fopen to read and analyze it. I want 3 seperate data sets from 3 different tabs, but I'm not sure if I just paste it all into a text file and I'm all set, or if I do something different. Hoping an expert here can help me, thanks.
Edit: I've attached the data and the excel file here for anyone to download.
close all
clear all
%User-defined Parameters
L= input('Enter a value for the length:');
w=input('Enter a value for the width: ') ;
h=input('Enter a value for the thickness: ');
%Calculating Area
A=w*h;
%Load text file
fid=fopen('Torsion.txt');
s=textscan(fid,'%f %f %f','headerlines',0);
fclose(fid);
F = s{1};%N
t = s{2}; %s
delta = s{3}; %mm
%Calculation of strain and stress
stress=F./A; %MPa
strain=delta./L; %mm/mm
%Plotting the Load-Deformation Curve
subplot(2,1,1)
hold on; grid on
plot(delta,F,'--b')
title('Load versus Deformation')
xlabel('\Delta, mm')
ylabel('Load, N')
%Plotting the Stress-Strain Curve
subplot(2,1,2)
hold on; grid on
plot(strain,stress,'r')
title('Stress Versus Strain')
xlabel('\epsilon, mm')
ylabel('\sigma, MPa')
hold off
%calculating modulus of elasticity from curve fitting
% evaluate polynomial fit result 1000 points in vetween strating and ending
% points
all_strain_points = strain(1):((strain(length(strain))-strain(1))/999):strain(length(strain));
all_stress_fit = zeros(1,1000);
% evaluate 1000 points in between initial and final points on spline fit
for i=1:1:1000
all_stress_fit(1,i) = feval(fitresult,all_strain_points(i));
end
l_s = length(strain);
% calculate value of modulus of elasticity
sss=zeros(length(l_s)-1); % initialize the variable to reduce processing time
for iii=1:1:(l_s-1)
% sss = slope of stress and strain graph
sss(iii) = (stress(iii+1)-stress(iii))/(strain(iii+1)-strain(iii));
end
% it might be possible that experimental data
% have some deviation with respect to
% exact slope, so 0.002(%) deviation is given as tolerance on variation of
% maximum strain slope difference.
for ii=1:1:(length(sss)-1);
slope_diff(ii) = sss(ii+1)-sss(ii);
end
tolerance = (0.00002*max(slope_diff));
pp=(abs(slope_diff)>abs(tolerance));
iii=1; % initialize the vsrisble for while loop
% findout proportional limit
while (pp(iii)==0)
iii=iii+1;
end
proportional_limit = stress(iii+1);
% E = modulus of elasticity
% iii+1 stores the proportional limit value
E = (proportional_limit-stress(1))/(strain(iii+1)-strain(1));
% Ulti_str = ultimate strength of material
Ult_str = max(all_stress_fit);
% yield limit
% it is assume that yield is at the yield_offset % of the strain
% for example 10 % of the ultimate strain
% line passing through 0.1*max(strain) as x coordinate and slope of E,
% stress = E*strain + intersept;
% since line is passing trough (0.1*max(strain),0);
% intersept = -0.1*max(strain)*E;
y_intersept = -(yield_offset_percent/100)*max(all_strain_points)*E;
x_intersept = (yield_offset_percent/100)*max(all_strain_points);
no_of_devision = abs((0-strain(length(strain)))/999);
strainpoints= 0:no_of_devision:strain(length(strain));
stress_line = E.*strainpoints + y_intersept;
% intersection can be findout by difference of the values of two curves
curve_diff = stress_line-all_stress_fit;
y_i = min(abs(curve_diff));
for ii=1:1:1000
incr = (y_i==(abs(curve_diff(ii))));
if incr == 1
yield_stress = stress_line(ii);
yield_strain = strainpoints(ii);
end
end
y_point=[yield_strain,yield_stress];
% calculate fracture strength
fracture_stress = all_stress_fit(1000);
% calculation of tfs = true fracture stress
% according to conservation of volume
% area at fracture can be find out
% from that (force/defored_area) is the true fracture stress
area_at_failure = w*h*(L/(L+(delta(length(delta)))));
tfs = F(length(F))/area_at_failure;
% Ploting the results
figure
plot(strainpoints,stress_line,'-r');
hold on
plot(all_strain_points,all_stress_fit,'--b');
hold on
plot(all_strain_points(1000),fracture_stress,'-ok');
hold on
plot(all_strain_points(1000),tfs,'-sm');
hold on
plot(y_point(1,1),y_point(1,2),'-sg');
xlabel( 'strain' );
ylabel( 'stress' );
title('Material Data Results')
grid on
legend('yield offset line','spline fit','Fracture Stress','True Fracture Stress','Yield Point');
fprintf('\n-------------------- Answer --------------------\n');
fprintf('Modulus of Elasticity E: %f\n',E);
fprintf('Proportional limit : %f\n',proportional_limit);
fprintf('Ultimate Strength : %f\n',Ult_str);
fprintf('Yield Strength at %2.2f percentage offset : %f\n',yield_offset_percent,yield_stress);
fprintf('Fracture Stress : %f\n',fracture_stress);
fprintf('True Fracture Stress : %f\n',tfs);
fprintf('-------------------- Answer --------------------\n');
1 commentaire
Walter Roberson
le 2 Déc 2022
If you already have the data in memory, then it is not clear to me why you would bother to write it to text file just to read it back in??
Réponses (1)
Sudarshan
le 5 Déc 2022
Hi Spencer,
You can store specific excel sheets in the MATLAB Workspace using the “readtable” function as shown in the below code snippet.
var = readtable('myfile.xlsx','Sheet','my sheet name');
You can refer to the documentation on “readtable” to know more:
You can save them separately in .txt files using the “writetable” commands if that is what you mean by creating datasets. You can refer to the below documentation on how to use “writetable”:
1 commentaire
Walter Roberson
le 5 Déc 2022
The user appears to be wanting to output into the same format as Torsion.txt which is not something writetable can do.
Voir également
Catégories
En savoir plus sur Live Scripts and Functions dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!