how to incorporating a dataset in the form of a struct into a function in a for loop

3 views (last 30 days)
i havee written the following code and getting error
fileName= {'Mert'};
nameExt= {'dataset.'};
snameExt= string(nameExt);
nfileName= string(fileName);
load('dataset.mat')
for i1= 1:length(nfileName)
movName={'AOG'};%,'FUZ','FUZY','MaW','NoW','OAK'};
nmovName= string(movName);
for i2=1:length(nmovName)
repName={'Bir','iki','uc'};
nrepName= string(repName);
for i3=1:length(nrepName)
fTime={'TO'};%'TS'};
nfTime= string(fTime);
for i4=1:length(nfTime)
segName= {'L5S1','L3L4','T12L1','T8T9','C7T1','C1Head','RT4Shoulder','RShoulder','RElbow','RWrist','LT4Shoulder','LShoulder','LElbow','LWrist',...
'RHip','RKnee','RAnkle','RBallFoot','LHip','LKnee','LAnkle','LBallFoot'};
nsegName=string(segName);
for i5= 1:length(nsegName)
rotName={'_x','_y','_z'};
nrotName=string(rotName);
for i6=1:length(nrotName)
infileName(i1)= snameExt+nfileName(i1)+'.'+nmovName(i2)+'.'+nrepName(i3)+'.'+nfTime(i4)+'.'+nsegName(i5)+nrotName(i6)+'.'+'jointAngle';
for k = 1: length(infileName)
new_data=[];
B=[];
B(k)= (infileName(k));
x = linspace(0, 100, length((infileName)));
y = 1:100;
new_data = spline(x, [(infileName) y]);
normalized.(infileName(k))=(new_data(k));
end
end
end
end
end
end
end
errors
Error using chckxy (line 42)
There should be at least two data points.
Error in spline (line 53)
[x,y,sizey,endslopes] = chckxy(x,y);
Error in incorporatingdataset (line 31)
new_data = spline(x, [(infileName) y]);
i also attached the dataset file

Accepted Answer

Walter Roberson
Walter Roberson on 2 Sep 2021
infileName(i1)= snameExt+nfileName(i1)+'.'+nmovName(i2)+'.'+nrepName(i3)+'.'+nfTime(i4)+'.'+nsegName(i5)+nrotName(i6)+'.'+'jointAngle';
The right hand side will be a string object that is probably intended to represent a file name
B(k)= (infileName(k));
with infileName being a string array, then B(k) will be a string scalar that is the file name (likely). But you do not use B after that anyhow.
new_data = spline(x, [(infileName) y]);
infileName is a vector of string objects. You use the entire string vector. When you [] that together with numeric y, then numeric y will be converted to string object using a process similar to as-if you had used compose("%g", y). WIth the [] that string vector would be added after the vector of string objects in infileName, creating a longer vector of string objects.
Then spline() would be called, passing in x as the first parameter, and passing in that string vector of filenames and y values as the second parameter. And that is a problem. Notice that you never read anything in from a file.
Ah, I see now, you are computing a path into a struct, as a string object. But you are not telling MATLAB to evaluate to get to the struct contents.
With that context, then:
infileName(i1)= snameExt+nfileName(i1)+'.'+nmovName(i2)+'.'+nrepName(i3)+'.'+nfTime(i4)+'.'+nsegName(i5)+nrotName(i6)+'.'+'jointAngle';
No, instead use
ThisData = dataset.(nfileName(i1)).(nmovName(i2)).(nrepName(i3)).(nfTime(i4)).(nsegName(i5)).(nrotName(i6)).jointAngle;
ThisData will then be the actual content. Do something appropriate with it. Store if you need to.
There are more efficient ways to process this instead of going down the full chain dynamically every time, but this will get you started.
  1 Comment
Muhammad
Muhammad on 2 Sep 2021
ThisData = dataset.(nfileName(i1)).(nmovName(i2)).(nrepName(i3)).(nfTime(i4)).(nsegName(i5)).(nrotName(i6)).jointAngle;
i have 40 patients data so i have 40 in fileName, i have 13 different movement patern in nmovName, i have 3 trials, i have 22 joints in nSegName, i have 3 axis in nrot name and all this data i recorded two times (pre-treatment-post treatment).
so i want to read each of this variables

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!