taking multiple column to form a struct

조회 수: 3 (최근 30일)
Sharah
Sharah 2015년 12월 21일
댓글: Guillaume 2015년 12월 21일
hello, I have a data set from text file, which is a 1x 13 size file. first I diff the data only to take the value of the data from clumn 13 (the counter) when it increases (some have repeated same numbers).
DataCube = textscan(D, '%f %f %f %f %f %f %f %f %f %f %f %f %f', 'HeaderLines', 13);
f = [true;diff(DataCube{1,13})~=0];
then when I try to extract the rest of the data into a struct such that:
Kid(1).Force{1,1:6} = DataCube{:, 1:6}(f==1);
the result returns: Expected one output from a curly brace or dot indexing expression, but there were 6 results. apart from extracting them individually, how else can I extract them in one go?

답변 (1개)

Guillaume
Guillaume 2015년 12월 21일
First, f==1 is a no-op, it's exactly the same as f.
Note that DataCube{:, 1:6} returns 6*number or rows individual values, so you would need as many output variables. It's probably not what you wanted to do. Possibly, this is what you wanted to do:
Kid.Force = DataCube(f, 1:6);
This will store the cell array only for those rows where f is true into a single field of your structure.
I looks like you're a bit confused by cell array syntax. My advice is to drop the cell array by converting it straight away to matrix, since it only holds scalar numerics:
DataCube = cell2mat(textscan(D, '%f %f %f %f %f %f %f %f %f %f %f %f %f', 'HeaderLines', 13));
f = [true;diff(DataCube(:,13))~=0]; %did you make a typo and replace : with 1?
  댓글 수: 2
Sharah
Sharah 2015년 12월 21일
i need the value of f when it is not equal to 0.
the solution still doesnt solve the problem of
Kid(subject).Force{trial,1:6} = DataCube(f, 1:6);
this is put in a double for loop, there are 6 components of force and i want to put them in different struct so that i can reach each data by Kid(1).Force{1, 2} for force 2 trial 1 kid 1
Guillaume
Guillaume 2015년 12월 21일
f is already logical (0s and 1s), so comparing it to 1 returns the exact same values: 0==1 -> 0, 1==1 -> 1
I do not understand what you're trying to do with your Force{trial, 1:6}. It looks like you are trying to distribute your 6 columns of the cell array to 6 columns of the Force field, but at the same time, f is likely to select several rows which you are trying to put into a single row (trial).
What in your DataCube cell array corresponds to a given force or trial?

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

카테고리

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