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

1 view (last 30 days)
Hello everyone!
I'm new in MATLAB. I want to analyze some kinematics and include some data in the struct into a new struct by including it in a function inside the for loop. I have lots of variables with different names. So, I wrote some codes with applicable this situation.
But when I try to run this code getting some errors. I have added codes and error. Please explain to me where i do wrong and how can i fix it. Thank you for your helps.
Here are some details:
I got the struct named "dataset" with another code.
My variables are struct which like "dataset.fileName.movName.repName.fTime.dataName.segName.rotName". The end point of this struct is a matris.now, I want to create a variable as a new struct by automatically inserting the matrix data in this "dataset.fileName.movName.repName.fTime.dataName.segName.rotName" structure into another function.
My transformation formula
%% My transformation formula
x = linspace(0, 100, length(your_data));
y = 0:100;
new_data = spline(x, your_data, y);
I want to put a " dataset.fileName.movName.repName.fTime.dataName.segName.rotName" variable in "your_data" section and create a new data like this " normalized.fileName.movName.repName.fTime.dataName.segName.rotName"
%% My code
fileName= {'name'};
nameExt= {'dataset.'};
snameExt= string(nameExt);
nfileName= string(fileName);
for i1= 1:length(nfileName)
movName={'HandToHead'};
nmovName= string(movName);
for i2=1:length(nmovName)
repName={'One','Two'};
nrepName= string(repName);
for i3=1:length(nrepName)
fTime={'TO'};%'TS'};
nfTime= string(fTime);
for i4=1:length(nfTime)
segName= {'RShoulder','RElbow','RWrist'};
nsegName=string(segName);
for i5= 1:length(nsegName)
rotName={'_x','_y','_z'};
nrotName=string(rotName);
for i6=1:length(nrotName)
dataName={'jointAngle','angularVelocity','angularAcceleration'};
ndataName= string(dataName);
for i7=1:length(ndataName)
infileName(i1)= snameExt+nfileName(i1)+'.'+nmovName(i2)+'.'+nrepName(i3)+'.'+nfTime(i4)+'.'+ndataName(i7)+'.'+nsegName(i5)+nrotName(i6);
for k=1:length(infileName)
A=[];
B=[];
B(k)= (infileName(k));
x = linspace(0, 100, length((B(k))));
y = 0:100;
A(k)= spline(x,(B(k)),y);
Normalized.(infileName(k))=(A(k));
end
end
end
end
end
end
end
end
Error text:
Error using chckxy (line 30)
The first two inputs must have at least two elements.
Error in spline (line 72)
[x,y,sizey,endslopes] = chckxy(x,y);
Error in normalize (line 33)
A(k)= spline(x,(B(k)),y);
  4 Comments
Stephen23
Stephen23 on 2 Sep 2021
Edited: Stephen23 on 2 Sep 2021
"...it is normal for me to write faulty "codes." (or what term you say rather than code)."
I did not write anything about "faulty" code.
I wrote that text that looks like code is not the same thing as code.
Your question shows that you joined lots of text together into something that looks the same as some code. But, as I wrote, some text (e.g. character vector, string) that looks like code is not code. It is certainly possible to evaluate that text as if it were code, but in almost all cases the much simpler and much more efficient approach is to write actual code (e.g. using the syntax in the link I gave you), not to create some text that looks like code.
"Have you any suggestion for this situation?"
Apparently you have nested structures and want to loop over their fieldnames.
As I wrote in my last comment, for that you can use dynamic fieldnames: see the link I gave you (it has examples).

Sign in to comment.

Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!