finding min and max values from a file
Show older comments
I am plotting a figure for disp. with time form several analysis results files and would like the software to find the max and min displacements and its loaction, how can I do that?
5 Comments
Dyuman Joshi
on 1 Oct 2023
Ahmed
on 1 Oct 2023
Dyuman Joshi
on 1 Oct 2023
Please attempt to determine the minimum and maximum values from this 9-point dataset. Could you kindly clarify your intended definition of displacement and its corresponding location? When I initially began using MATLAB, the term "Location" could encompass a distinct meaning within the context of computer programming, which can vary significantly from a purely mathematical continuous-time perspective. This concept was not covered in any of my math classes.
t = linspace(0, 10, 9)
y = sin(pi/5*t)
dpb
on 1 Oct 2023
The first unknown (for us) here is what form the files are...<you'll have to read each of them in turn...>
Answers (2)
Image Analyst
on 1 Oct 2023
Try this. It loops over all files and finds the min and max displacement of each file, along with the times that those extrema occurred. Untested code (adapt as needed):
% Specify the folder where the files live.
myFolder = 'C:\Users\yourUserName\Documents\My data';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isfolder(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s\nPlease specify a new folder.', myFolder);
uiwait(warndlg(errorMessage));
myFolder = uigetdir(); % Ask for a new one.
if myFolder == 0
% User clicked Cancel
return;
end
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.csv'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
numFiles = numel(theFiles)
% Preallocate results vectors.
minValues = nan(numFiles, 1);
maxValues = nan(numFiles, 1);
minIndexes = nan(numFiles, 1);
maxIndexes = nan(numFiles, 1);
timesOfMin = nan(numFiles, 1);
timesOfMax = nan(numFiles, 1);
% Loop over all files.
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
% Now do whatever you want with this file name,
% such as reading it in readmatrix and finding the max and min.
data = readmatrix(fullFileName);
% Get the times from column 1.
timeVector = data(:, 1);
% Get the displacements from column 2.
displacements = data(:, 2);
% Plot the displacements for this one file.
plot(timeVector, displacements, 'b-')
drawnow; % Force display to update immediately.
% Find the min and max displacement values for this one file.
[minValues(k), minIndexes(k)] = min(displacements);
[maxValues(k), maxIndexes(k)] = max(displacements);
% Find the times for those min and max displacements for this one file.
timesOfMin(k) = timeVector(minIndexes(k));
timesOfMax(k) = timeVector(maxIndexes(k));
end
% Plot the mins and maxes for all the files.
figure;
plot(minValues, 'b.-');
hold on;
plot(maxValues, 'b.-');
xlabel('File Number');
grid on;
% Get the overall mean over all the files that we read in.
[overallMinDsplacement, minFileIndex] = min(minValues);
[overallMaxDsplacement, maxFileIndex] = max(maxValues);
3 Comments
Ahmed
on 2 Oct 2023
Image Analyst
on 2 Oct 2023
I don't know. Try it. If it works, it works. This may help you figure that out:
Image Analyst
on 5 Oct 2023
If this Answer solves your original question, then could you please click the "Accept this answer" link to award the answerer with "reputation points" for their efforts in helping you? They'd appreciate it. Thanks in advance. 🙂 Note: you can only accept one answer (so pick the best one) but you can click the "Vote" icon for as many Answers as you want. Voting for an answer will also award reputation points.
Ahmed
on 5 Oct 2023
0 votes
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!