为什么我的代码没有生成任何图表,而且没有报错?

huangapple go评论64阅读模式
英文:

Why is my code not generating any figures, without it giving errors?

问题

以下是您提供的代码的中文翻译部分:

% 步骤 1:定义包含您的文本文件的目录
directory = 'C:\Users\Biologisch';

% 步骤 2:获取目录中的文件列表
files = dir(directory);

% 定义要提取的列索引
columnIndices = [1, 2, 3];  % 根据您的需求进行修改

% 为结果列表定义自定义名称
listNames = {'Crosshead', 'Load', 'Time'};  % 使用您的自定义名称进行修改
all_E = [];

% 步骤 3:使用自定义名称初始化结果列表,以存储每列的结果
numColumns = numel(columnIndices);
resultLists = cell(1, numColumns);
for i = 1:numColumns
     resultLists{i} = [];
end

W0 = 45;               % 开始宽度 [mm]
D = 4*10^-3;           % 厚度 [mm]
W = 40;                % 结束宽度
Area = (D)*(W0)*10^(-6);

% 要跳过的行数(在此情况下为 8 行)
linesToSkip = 8;

% 将 divisionResultsList 初始化为空的单元格数组
divisionResultsList = cell(1, length(files));

% 步骤 4:迭代文件列表
set(0, 'DefaultFigureVisible', 'on');
for i = 1:length(files)
    filename = files(i).name;
    
    % 步骤 5:检查文件是否为文本文件
    if endsWith(filename, '.txt')
        
        % 打开文本文件
        fileID = fopen(fullfile(directory, filename));
        
        % 跳过前 8 行
        for j = 1:linesToSkip
            fgetl(fileID);
        end
        
        % 使用 textscan 读取剩余的行
        data = textscan(fileID, '%f');
        
        % 关闭文件
        fclose(fileID);
        
        % 计算应力和应变
        crossheadColumn = data{columnIndices(1)};
        loadColumn = data{columnIndices(2)};
        Stress = loadColumn ./ Area;
        L0 = crossheadColumn(1);
        % 从 crosshead 数据计算应变
        Strain_length = (crossheadColumn-L0)/L0;   
        Strain_width = (W0-W)/W;

        % 将除法结果存储在 divisionResultsList 中
        divisionResultsList{i} = Stress;
        
        p = polyfit(Strain_length,Stress,1);
        f = polyval(p,Strain_length);     % 在首选的 x 坐标上拟合多项式
        E    = p(1);
        all_E = [all_E;E];
        
        % 绘制应力-应变曲线
        figure(1)
        plot(Strain_length,Stress,'b')
        hold on 
        plot(Strain_length,f,'r')
        xlabel('应变')
        ylabel('应力 [MPa]')
        title(['文件: ', files(i).name])
        legend('应力-应变曲线','线性拟合')
        hold off

        % 保存图形到文件夹
        saveas(gcf, fullfile(directory, '应力-应变曲线', [filename '.png']));
    end
end

请注意,以上翻译只包括代码部分,不包括代码之外的任何内容。

英文:

I have data from several planar uniaxial tests and want to plot stress-strain figures and put the Young's modulus, which is defined as the slope of the stress-strain graph, in a list that will eventually contain all young's moduli.

I wrote the following code, but matlab did not generate any figures or saved any to my computer:

% 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

得分: 1

1.- MATLAB和Microsoft DOS命令dir的工作方式相似:

两者都会获取文件夹中的所有文件,以构建所请求的列表。

可能会出现这样的情况,即文件.(进入下一级)和..(返回上一级)也包含在您使用的原始文件列表中。

我以前遇到过类似的问题,解决方法是排除文件...

然后,您可以开始处理包含数据的任何文件。

2.- 此外,如果您与问题的读者分享一个包含数据的文件,将其附加到问题中,或至少提供一个样本,那么读者就可以评估文件中的数据是否被正确读取,或是否存在其他问题。

英文:

1.- The MATLAB and Microsoft DOS commanddir both work alike:

Both take all the files in the folder, to build the requested list.

It may be the case that the files . (go 1 level down) and .. (go 1 level up) are also included in the raw list of files that you use.

I had a similar problem time ago and the solution is to exclude files . and ..

Then you can start processing whatever files containing data.

2.- Also, if you share with the readers of your question one file with data, attaching it to the question, or at least a sample of it, then readers will be a assess whether the data in the file is read correctly, or any other point.

huangapple
  • 本文由 发表于 2023年6月12日 20:50:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76456841.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定