조회 수: 6 (최근 30일)
caroline bourn
caroline bourn . 2021년 3월 11일
댓글: caroline bourn . 2021년 3월 22일
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;
% 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);
if pts(i) == length(pts)
fin = 0;
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
caroline bourn
caroline bourn 2021년 3월 16일
thanks for the tip :) i'll keep it in mind :)!!

댓글을 달려면 로그인하십시오.

채택된 답변

Athul Prakash
Athul Prakash 2021년 3월 16일
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
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;
% '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
caroline bourn
caroline bourn 2021년 3월 22일
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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)


Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기




Community Treasure Hunt

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

Start Hunting!

Translated by