如何同时在stress、strain两个文件夹的同名.csv文件上运行MATLAB脚本?
Got it, let's break this down into actionable steps to get your script working seamlessly with the stress and strain folders. Since your filenames are perfectly matched across both directories, we can leverage that pairing to process each sample's data together without hassle.
Step 1: Grab the List of Matching Sample Files
First, we'll pull the CSV file list from one folder (say, stress), then use those base filenames to fetch the corresponding files from strain. This ensures we only process pairs that exist in both locations.
% Define your folder paths (adjust to match your actual directory structure) stressFolder = './stress/'; strainFolder = './strain/'; % Get all CSV files in the stress folder stressFiles = dir(fullfile(stressFolder, '*.csv')); % Extract base filenames (remove the .csv extension) to match across folders sampleNames = {stressFiles.name}; sampleNames = cellfun(@(x) x(1:end-4), sampleNames, 'UniformOutput', false);
Step 2: Loop Through Each Sample & Process Data
Now we'll iterate over every sample, read both stress and strain data, then plot the stress-strain curve. I've added some error checking here to handle edge cases (like a missing file in one folder) so your script doesn't crash unexpectedly.
% Optional: Create a folder to save plots (auto-make if it doesn't exist) outputPlotFolder = './stress_strain_plots/'; if ~exist(outputPlotFolder, 'dir') mkdir(outputPlotFolder); end % Set up a figure for subplots (adjust layout based on your number of samples) figure('Position', [100 100 1200 800]); subplotIdx = 1; totalSamples = length(sampleNames); subplotRows = ceil(totalSamples / 2); % 2 columns of subplots for i = 1:totalSamples currentSample = sampleNames{i}; % Build full paths to the matching files stressPath = fullfile(stressFolder, [currentSample '.csv']); strainPath = fullfile(strainFolder, [currentSample '.csv']); % Skip if either file is missing if ~exist(stressPath, 'file') || ~exist(strainPath, 'file') warning('Skipping %s: Missing stress or strain file', currentSample); continue; end % Read data (use readtable instead if your CSVs have headers) stressData = readmatrix(stressPath); strainData = readmatrix(strainPath); % Check if data lengths match (avoid plotting mismatched datasets) if size(stressData, 1) ~= size(strainData, 1) warning('Skipping %s: Stress and strain data have different lengths', currentSample); continue; end % Plot the stress-strain curve subplot(subplotRows, 2, subplotIdx); plot(strainData, stressData, 'LineWidth', 1.5, 'Color', rand(1,3)); title(['Sample: ' currentSample], 'FontSize', 10); xlabel('Strain'); ylabel('Stress'); grid on; box on; % Save individual plot for the sample saveas(gcf, fullfile(outputPlotFolder, [currentSample '_curve.png'])); subplotIdx = subplotIdx + 1; end % Optional: Save the full figure with all subplots saveas(gcf, fullfile(outputPlotFolder, 'all_stress_strain_curves.png'));
Step 3: Customize to Fit Your Data
- If your CSV files have column headers, swap
readmatrixforreadtable, then extract the relevant columns (e.g.,stressValues = table2array(stressTable(:, 'Stress'));). - Adjust the subplot layout (change
subplotRowsor switch to a single column if you prefer). - Tweak plot styles (colors, markers, line thickness) to match your reporting needs.
- If you want all curves on a single plot instead of subplots, remove the
subplotcalls, usehold onat the start, and add a legend withlegend(sampleNames).
内容的提问来源于stack exchange,提问作者enea19

