Loop through ,mat files and find a specific column with conditions
2 次查看(过去 30 天)
显示 更早的评论
I am trying to write a code to loop through 50 .mat subjects, in each subject find right and left foot, and compare three columns of left with the right foot.
If it the data in those column is similar, write a specific column, which is a matrix in a different location and save them.
But it does not read textData as matrix or rather a set of data. It reads it just as a column. Am i opening textData wrong?
%get each .mat data and routate trough it
folder=cd;
for k = 1:50
matFilename = sprintf('Subject%d.mat', k); % _CD_ add %d to add the number to the string
matData = load(fullfile(cd, matFilename)); % _CD_ matData is not used. What is it for?
% _CD_ What is this File? Did you prepare the data before?
fid = fopen(fullfile(folder, matFilename), 'rt');
textData = fread(fid);
sz = size(textData);%how many rows has the .mat data?
L = []; %create array for left foot
R = []; %create array for right foot
ang = [];
for i = 1:sz %assigne the left to an array and right as well
if strcmp(textData(i,1) ,'LX') %find the left foot
L = textdata(i,:);
elseif strcmp(textData(i,1) ,'RX') %find the right foot
R = textdata(i,:);
end
end
%compaee left and right speed strideLength stepWidth
szR= size(R); %size of right matrix
szL= size(L); %size of left matrix
for i =1:szR
for j =1:szL
% if (isMatch == abs(L(i,9)-R(i,9)) < 0.1) && (isMatch == abs(L(i,10)-R(i,10)) < 0.1) && (isMatch == abs(L(i,11)-R(i,11)) < 0.1);
if (abs(L(j,9)-R(i,9)) < 0.1) && (abs(L(j,10)-R(i,10)) < 0.1) && (abs(L(j,11)-R(i,11)) < 0.1)
%assign the specific column and save
rbh_bend = R(i).Ang(4,:);
rbk_bend = R(i).Ang(7,:);
%check if rbk_bend is negative
% _CD_ all Values have to be inverted not only the positive ones. But because all are positive this should be no problem.
rbk_bend = rbk_bend * -1;
rbf_bend = R(i).Ang(10,:);
ang(1,:) = rbh_bend;
ang(2,:) = rbk_bend;
ang(3,:) = rbf_bend;
lbh_bend = L(j).Ang(4,:);
lbh_bend = fliplr(lbh_bend);%flip 1-50 50-100
lbk_bend = L(j).Ang(7,:);
% _CD_ same as above. just invert all without the if
lbk_bend = lbk_bend * -1;
lbk_bend = fliplr(lbk_bend);%flip 1-50 50-100
lbf_bend = L(j).Ang(10,:);
lbf_bend = fliplr(lbf_bend);%flip 1-50 50-100
ang(4,:) = rbh_bend;
ang(5,:) = rbk_bend;
ang(6,:) = rbf_bend;
names = ["rbh_bend ","rbk_bend ","rbf_bend ";
"lbh_bend ","lbk_bend","lbf_bend "];
File_name = sprintf(textFilename,k,i);
save(File_name, "ang", "names") % save two different files one names and the other the %data of ang for each right and left foot with similar speed (similar three column) of each subject.
end
end
end
fclose(fid);
end
2 个评论
Stephen23
2022-10-12
save(File_name, "ang", "names") % save two different files one names and the other...
Actually saves one file with two variables in it.
Getting terminology correct goes a long way to understanding what is going on.
回答(1 个)
Benjamin Thompson
2022-10-10
If you have MAT files you load them using the load function. No need to use fopen or fread.
5 个评论
Stephen23
2022-10-12
编辑:Stephen23
2022-10-12
As Benjamin Thompson correctly wrote, MAT files are imported using LOAD, not by fiddling with file bits.
It is strongly recommended that you LOAD into an output variable (which is a sclalar structure), e.g.:
P = 'absolute or relative path to where the files are saved';
F = sprintf('Subject%d.mat', k);
S = load(fullfile(P,F));
D = S.Data % get e.g. the DATA field
Once you answer Benjamin Thompson's questions here, it will be much easier to help you. Your screenshot here appears to show 17 fields of a scalar structure, but none of those fields are anything "like a matrix with 16 columns and different number of rows", so it is unclear what you are referring to with that comment.
In any case, The code I gave above to access the DATA field of the scalar structure of the LOADed data, if that is in fact what you really have. Even better would be if you uploaded a sample data file.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Structures 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!