finding min and max values from a file

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

Use min and max functions.
What is the command that I should write to get it and also cite it on the figure?
I have linked the commands you need to use. Click on them to see how to use/implement them.
To cite the points in figure, use plot or scatter
Hi @Ahmed,
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)
t = 1×9
0 1.2500 2.5000 3.7500 5.0000 6.2500 7.5000 8.7500 10.0000
y = sin(pi/5*t)
y = 1×9
0 0.7071 1.0000 0.7071 0.0000 -0.7071 -1.0000 -0.7071 -0.0000
The first unknown (for us) here is what form the files are...<you'll have to read each of them in turn...>

Sign in to comment.

Answers (2)

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

Can it work with my code below? For each file, the first column for time and the second one is for displacement.
%% file 1
file_path1 = 'C:\Users\Ahmed\Desktop\Displacement_A1.txt';
file_id1 = fopen(file_path1, 'r');
n1 = 8002; % number of line
data1 = textscan(file_id1, '%f %f', n1);
fclose(file_id1);
column11 = data1{1};
column12 = data1{2};
%% file 2
file_path2 = 'C:\Users\Ahmed\Desktop\Displacement_A2.txt';
file_id2 = fopen(file_path2, 'r');
n2 = 8002; % number of line
data2 = textscan(file_id2, '%f %f', n2);
fclose(file_id2);
column21 = data2{1};
column22 = data2{2};
%% file 3
file_path3 = 'C:\Users\Ahmed\Desktop\Displacement_A3.txt';
file_id3 = fopen(file_path3, 'r');
n3 = 8002; % number of line
data3 = textscan(file_id3, '%f %f', n3);
fclose(file_id3);
column31 = data3{1};
column32 = data3{2};
%% file 4
file_path4 = 'C:\Users\Ahmed\Desktop\Displacement_A4.txt';
file_id4 = fopen(file_path4, 'r');
n4 = 8002; % number of line
data4 = textscan(file_id4, '%f %f', n4);
fclose(file_id4);
column41 = data4{1};
column42 = data4{2};
%%
plot(column11, column12, '--', column21, column22, '--', column31, column32, '--', column41, column42);
legend('A1','A2','A3','A4');
title('Displacement in X-Direc.'); % figure title
xlabel('Time (s)');
ylabel('Disp. (m)');
grid on;
I don't know. Try it. If it works, it works. This may help you figure that out:
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.

Sign in to comment.

Ahmed
Ahmed on 5 Oct 2023
Thank you all for your kind and helpful answer.

Asked:

on 1 Oct 2023

Commented:

on 5 Oct 2023

Community Treasure Hunt

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

Start Hunting!