MATLAB Answers

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

2 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

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 (0)

Community Treasure Hunt

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

Start Hunting!