我有几个平面单轴测试的数据,我想绘制应力应变图,并把杨氏模量(定义为应力应变图的斜率)放在一个最终包含所有杨氏模量的列表中。
我写了下面的代码,但是matlab没有生成任何图形,也没有保存到我的计算机上:
% Step 1: Define the directory where your text files are located
directory = 'C:\Users\Biologisch';
% Step 2: Get a list of files in the directory
files = dir(directory);
% Define the column indices you want to extract
columnIndices = [1, 2, 3]; % Modify this as per your requirement
% Define custom names for the result lists
listNames = {'Crosshead', 'Load', 'Time'}; % Modify this with your custom names
all_E = [];
% Step 3: Initialize result lists with custom names to store the results for each column
numColumns = numel(columnIndices);
resultLists = cell(1, numColumns);
for i = 1:numColumns
resultLists{i} = [];
end
W0 = 45; %Breedte begin [mm]
D = 4*10^-3; %Dikte [mm]
W = 40; %Breedte eind
Area = (D)*(W0)*10^(-6);
% Number of lines to skip (in this case, 8 lines)
linesToSkip = 8;
% Initialize divisionResultsList as an empty cell array
divisionResultsList = cell(1, length(files));
% Step 4: Iterate over the list of files
set(0, 'DefaultFigureVisible', 'on');
for i = 1:length(files)
filename = files(i).name;
% Step 5: Check if the file is a text file
if endsWith(filename, '.txt')
% Open the text file
fileID = fopen(fullfile(directory, filename));
% Skip the first 8 lines
for j = 1:linesToSkip
fgetl(fileID);
end
% Read the remaining lines using textscan
data = textscan(fileID, '%f');
% Close the file
fclose(fileID);
% Calculation of stress and strain
crossheadColumn = data{columnIndices(1)};
loadColumn = data{columnIndices(2)};
Stress = loadColumn ./ Area;
L0 = crossheadColumn(1);
%calculate strain from crosshead data
Strain_length = (crossheadColumn-L0)/L0;
Strain_width = (W0-W)/W;
% Store the division results in the divisionResultsList
divisionResultsList{i} = Stress;
p = polyfit(Strain_length,Stress,1);
f = polyval(p,Strain_length); % To fit the polynomial on the preferred x coordinates
E = p(1)
all_E = [all_E;E];
%plot stress-strain curve
figure(1)
plot(Strain_length,Stress,'b')
hold on
plot(Strain_length,f,'r')
xlabel('Strain')
ylabel('Stress [MPa]')
title(['File: ', files(i).name])
legend('Stress-strain curve','Linear fit')
hold off
%save plot to folder
saveas(gcf, fullfile(directory, 'Stress-strain curve', [filename '.png']));
end
end
1条答案
按热度按时间inkz8wg91#
**1.-**MATLAB和Microsoft DOS命令
dir
的工作方式相同:两者都获取文件夹中的所有文件,以构建请求的列表。
可能的情况是,文件
.
(向下一级)和..
(向上一级)也包含在您使用的原始文件列表中。我以前也遇到过类似的问题,解决方案是排除文件
.
和..
然后,您可以开始处理任何包含数据的文件。
**2.-**此外,如果您与问题的读者共享一个带有数据的文件,将其附加到问题中,或者至少是其中的一个样本,那么读者将评估文件中的数据是否正确读取,或者任何其他点。