Trying to compile raw data from 30 tests to form an average stress strain curve.

20 views (last 30 days)
I am trying to access 30 different excel files, extract 2 columns from each, calculate the max from each column, and then make a graph showing the curve of all tests combined.
Below is my code so far. I get an output of NaN. Does anyone have any ideas on why it wont output the mean of the maximums?
% MATLAB Code to Extract Max from Column 7 and 8, and then calculate the mean of those maximums
File = fullfile('dC:\Users\tkite\OneDrive\Desktop\Logan Data\Toray_P25');
LIST = dir(fullfile(File, '*.xlsx')); % Filter for Excel files
isDir = [LIST.isdir];
LIST = LIST(~isDir);
N = length(LIST);
% D will store all the maximums: Col 1 for Max Col 7, Col 2 for Max Col 8.
Ncols_result = 2;
D = zeros(N, Ncols_result);
% --- LOOP TO EXTRACT ALL MAXIMUMS ---
for ii = 1:N
FFName = fullfile(LIST(ii).folder, LIST(ii).name);
Data = readmatrix(FFName);
if ~isempty(Data)
% Extract the maximum value from Column 7 (all rows)
Max_Col_7 = max(Data(:, 7));
% Extract the maximum value from Column 8 (all rows)
Max_Col_8 = max(Data(:, 8));
% Store the two single max values into the results matrix D
D(ii, 1) = Max_Col_7;
D(ii, 2) = Max_Col_8;
else
% Handle empty files with NaN
D(ii, :) = [NaN, NaN];
end
end
% --- FINAL CALCULATION: MEAN OF ALL MAXIMUMS ---
% The mean() function, when given a column vector, calculates the average of its elements.
% D(:, 1) is the column vector of all Max_Col_7 values (one for each file)
mean_max_col7 = mean(D(:, 1), 'omitnan');
% D(:, 2) is the column vector of all Max_Col_8 values
mean_max_col8 = mean(D(:, 2), 'omitnan');
% Display the final results
disp(' ');
disp(' FINAL MEAN OF MAXIMUM VALUES');
disp(['Average of MAX values for Column 7: ', num2str(mean_max_col7, '%.4f')]);
disp(['Average of MAX values for Column 8: ', num2str(mean_max_col8, '%.4f')]);

Answers (1)

dpb
dpb about 3 hours ago
Edited: dpb about 1 hour ago
NOTA BENE:
The path below has an extraneous 'd' at the beginning; if this is present in your real case the subsequent directory listing will be empty and the loop won't run at all, but the code after the loop will still try to run.
File = fullfile('dC:\Users\tkite\OneDrive\Desktop\Logan Data\Toray_P25');
Make sure the above typo is fixed first...
% MATLAB Code to Extract Max from Column 7 and 8, and then calculate the mean of those maximums
File = fullfile('C:\Users\tkite\OneDrive\Desktop\Logan Data\Toray_P25');
LIST = dir(fullfile(File, '*.xlsx')); % Filter for Excel files
% don't need the following assuming you don't name folders with the Excel file extensions
%isDir = [LIST.isdir];
%LIST = LIST(~isDir);
N=numel(LIST); % numel() is a little more robust than length()
% D will store all the maximums: Col 1 for Max Col 7, Col 2 for Max Col 8.
Ncols_result = 2;
D=nan(N, Ncols_result); % preallocate with NaN for missing values
for ii = 1:N
FFName = fullfile(LIST(ii).folder, LIST(ii).name);
Data = readmatrix(FFName);
if ~isempty(Data)
% Extract the maximum value from Columns 7 and 8
D(ii,:)=max(Data(:,[7:8]));
else
warning('File %s missing data.',FFName)
end
end
% --- FINAL CALCULATION: MEAN OF ALL MAXIMUMS ---
% Sanity check on data --
assert(~all(isfinite(D),'all'),'ERROR: No max values were finite. Aborting.')
Max_Mean=mean(D,'omitnan'); % use vector addressing of MATLAB
% Display the final results
disp(' ');
disp(' FINAL MEAN OF MAXIMUM VALUES');
fprintf('Average of MAX values for Column %d: %0.4f\n', 7, Max_Mean(1),8, Max_Mean(2))
As @Mathieu NOE notes, since this is undoubtedly related to the data, would have to have it in order to be able to say definitively what went wrong. It initializes the D array to all NaN to start with so you can look at it after the loop and see if there are data files missing any data as well as echoes any such files to the command line.
The above will give you an error if all the data turn out to be non-finite which is what is possible with your code if for some reason the second path of your if clause were true.
  2 Comments
dpb
dpb about 1 hour ago
Edited: dpb about 1 hour ago
No problem; glad to help -- and I didn't see it until later, myself.
Key point is to set a breakpoint in the code and step through it to discover where things aren't what are expected.
If this solved the problem, be good to Accept the Aanswer to let others know, if nothing else...

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!