Clear Filters
Clear Filters

.MAT FILES AND WRITING CODE TO ANALYSE THE .MAT FILES

43 views (last 30 days)
Hello everyone. i am going to try give you as much information as possible. i know it may seem like a simple question but i am ripping my hair out.
ok so my original txt file has over 3000 rows of data and 5 columns.i have written a code that goes through the time (column 2) and identifies a change. That change in time corresponds to the start of a new test. For the original text file i found i have 3 tests, hence, 3 .mat files that i have saved using the following script. Each .mat file has 3 columns and multiple rows and each .mat file corresponds to a seperate test (like i mentioned). the columns are x,y, test condition 1 -18 etc.
pts = findchangepts(data(:,2), 'Statistic', 'linear', 'MinThreshold',100)
pts(end+1) = finalrow;
num_step = data(:,1)
x = data(:,4);
y = data(:,5);
fin = 1;
next = 1;
while(fin)
% make seperate files for the tests.
for i=1:length(pts)
matrix = [x(next:pts(i,:)), y(next:pts(i,:)), test_cond(next:pts(i,:))];
filename = sprintf('%s_%d','test',i);
save(filename, 'matrix')
next = pts(i);
end
if pts(i) == length(pts)
fin = 0;
end
return
end
NOW i want to write code to do whatever it is i want it to do and i want it to do it to each mat file.
i.e. go through each test and find all the zeroes, save those in a seperate file blah blah blah.
i know i have to use a loop but some advice as to the best and most efficent way to execute this would be great.
my coding is not fantastic so please, if you dont have anything useful or nice to contribute dont do it at all. thanks.
  2 Comments
Athul Prakash
Athul Prakash on 16 Mar 2021
Hi Caroline, quick tip..
When trying to get help on this platform, it helps to ask a specific question and leave out details that are not needed. If the question can be understood by a third party in quick time, you are far more likely to receive a response. Also, please refrain from capitalizing the headline or using imperative language in the question, as that is something most people in the community try to avoid and hence your question may go unanswered.
I found the following page to be a good guideline when I'm on this platform:-

Sign in to comment.

Accepted Answer

Athul Prakash
Athul Prakash on 16 Mar 2021
Hi Caroline,
You may consider using struct arrays to group your data, and save everything to one file as a simplification.
% Consider one struct for each 'test' that you mention, each struct having one field say 'data'\
tests = struct() % initialize an empty 1x1 struct
% variable 'seps' - separators, ending indices of each
% variable 'data' - 2D array to split up row-wise
start=1
for i=1:length(seps)
% add the data for i-th test to the field called 'data' in i-th structure
tests(i).data = data(start:seps(i), :);
start = seps(i)+1;
end
% 'tests' is a (numTests x 1) dimensional array of structures, each
% having one field 'data'
save('test.mat', tests);
You may read the following documentation to get a grip of MATLAB structures
It's like a structure in C, where you can group data in different 'fields' (such as 'data' above) and access using dot notation.
But MATLAB has structure arrays, which means you can group many structures with same fields into an array of any shape.
Hope it helps!
  1 Comment
caroline bourn
caroline bourn on 22 Mar 2021
hey thanks for your comment! i managed to find a way to do it.
i used cells and then concatenated. my supervisor informed me it would be the best approach for my situation.
i did try your way and it was very useful! thank you

Sign in to comment.

More Answers (1)

Samuel
Samuel on 22 May 2024
% MATLAB Script for Noise and Vibration Analysis
% Created on: [Your Creation Date]
% Author: [Your Name]
% Load data from the provided MAT-files
data1 = load('data1.mat');
data2 = load('data2.mat');
% Assuming the data contains a variable named 'signal' and 'time'
signal1 = data1.signal;
time1 = data1.time;
signal2 = data2.signal;
time2 = data2.time;
% Plotting the signals
figure;
subplot(2, 1, 1);
plot(time1, signal1);
title('Signal 1');
xlabel('Time (s)');
ylabel('Amplitude');
subplot(2, 1, 2);
plot(time2, signal2);
title('Signal 2');
xlabel('Time (s)');
ylabel('Amplitude');
% Perform Fourier Transform to analyze the frequency components
Fs = 1000; % Sampling Frequency (Assumed)
% FFT for Signal 1
L1 = length(signal1);
Y1 = fft(signal1);
P2_1 = abs(Y1/L1);
P1_1 = P2_1(1:L1/2+1);
P1_1(2:end-1) = 2*P1_1(2:end-1);
f1 = Fs*(0:(L1/2))/L1;
% FFT for Signal 2
L2 = length(signal2);
Y2 = fft(signal2);
P2_2 = abs(Y2/L2);
P1_2 = P2_2(1:L2/2+1);
P1_2(2:end-1) = 2*P1_2(2:end-1);
f2 = Fs*(0:(L2/2))/L2;
% Plot the single-sided amplitude spectrum
figure;
subplot(2, 1, 1);
plot(f1, P1_1);
title('Single-Sided Amplitude Spectrum of Signal 1');
xlabel('Frequency (Hz)');
ylabel('|P1(f)|');
subplot(2, 1, 2);
plot(f2, P1_2);
title('Single-Sided Amplitude Spectrum of Signal 2');
xlabel('Frequency (Hz)');
ylabel('|P1(f)|');
% Calculate RMS value
rms_signal1 = rms(signal1);
rms_signal2 = rms(signal2);
% Display RMS values
fprintf('RMS value of Signal 1: %.2f\n', rms_signal1);
fprintf('RMS value of Signal 2: %.2f\n', rms_signal2);
% Filter design - Low-pass filter as an example
fc = 50; % Cut-off frequency
[b, a] = butter(6, fc/(Fs/2));
% Apply the filter to both signals
filtered_signal1 = filter(b, a, signal1);
filtered_signal2 = filter(b, a, signal2);
% Plot filtered signals
figure;
subplot(2, 1, 1);
plot(time1, filtered_signal1);
title('Filtered Signal 1');
xlabel('Time (s)');
ylabel('Amplitude');
subplot(2, 1, 2);
plot(time2, filtered_signal2);
title('Filtered Signal 2');
xlabel('Time (s)');
ylabel('Amplitude');
% Save results
save('filtered_signals.mat', 'filtered_signal1', 'filtered_signal2');

Categories

Find more on Structures in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!