Problem with for-loop

1 view (last 30 days)
Hans Janssen
Hans Janssen on 4 Jan 2015
Edited: Stephen23 on 4 Jan 2015
Hi Guys,
For my assignment I want to create a mean path velocity. I want to use two for-loops. 1 to go trough every subject and 1 to go trough every file. My files are stored in data(i_pp).files_c3d. When I run my program I get the error
Attempt to reference field of non-structure array.
Error in JonasExam (line 134) C=diff(data(i_pp).files_c3d(I).sts_markers.STRN(:,:,1))/(1/VideoFrameRate);
This is strange because A=.... is of the same kind. As you can see in my folder the map consists of 2 kinds of experiments. The first 5 and the last 3, is it possible that the warning is because C=.... isn't able to find sts_markers which starts from the 6th experiment.
What should I do with my for-loop to make sure the part after else starts reading from the 6th file?
  1 Comment
Stephen23
Stephen23 on 4 Jan 2015
The code .../(1/VideoFrameRate) could be simplified too.

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 4 Jan 2015
Edited: Stephen23 on 4 Jan 2015
The field data(x).files_c3d(y).sts_markers contains two different kind of data: empty arrays and structs. So when you reference the field ...sts_markers.STRN and the data is an empty array, what do you expect it to do? If it is an empty array then there is no field for that location, thus the error. For the locations containing a structure, this syntax will work without error.
The solution is to subset your structure first using some indexing. You can have a look at my answer to this (almost identical) question here, which shows how you can identify the parts of the structure that you require:
Good luck with your homework!
  2 Comments
Hans Janssen
Hans Janssen on 4 Jan 2015
Another problem is that the number of experiments change per subject, so it is possible that another subject has 4 empty slots before there is an array with data. Is it possible to let it start from the first array of data it encounters?
Stephen23
Stephen23 on 4 Jan 2015
Edited: Stephen23 on 4 Jan 2015
Of course, the indexing method I gave in that link resolves this automatically.
Note that it assumes that the empty arrays occur IFF 'name' is 'stair...', otherwise you can detect empty arrays directly:
X = cellfun('isempty',{data(x).files_c3d(y).sts_markers})
You can then use X to index out only the parts of the structure that you require. You might like to read the documentation on this:

Sign in to comment.

More Answers (0)

Categories

Find more on Parallel for-Loops (parfor) 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!