0

I have a struct array field seg.startscan. I want to erase some of its elements.

For example the elements corresponding to the indices stored contained in an array INDS.

The followings are ny try and the relative errors:

1)

seg.startscan(INDS) = [ ];

??? Scalar structure required for this assignment.

2)

seg(INDS).startscan = [ ];

??? Insufficient outputs from right hand side to satisfy comma separated list expansion on left hand side. Missing [] are the most likely cause.

3)

startscans = seg.startscan; startscans(INDS) = [ ]; fldnm = 'startscan'; seg.(fldnm) = startscans;

??? Insufficient outputs from right hand side to satisfy comma separated list expansion on left hand side. Missing [] are the most likely cause.

4)

startscans = seg.startscan; startscans(INDS) = [ ]; fldnm = 'startscan'; [seg.(fldnm)] = startscans;

??? Too many output arguments.

5) as suggested here: 1:

>>startscans = seg.startscan;
startscans(INDS) = [ ];
fldnm = 'startscan';
[seg.(fldnm)] = startscans;

??? Too many output arguments.

Do you have any ideas? Probably I do not grasp the idea behind the struct arrays..

Let's say that my input is:

>>seg.startscan
ans=
1
ans=
2
ans=
3
ans=
4
>>INDS = [1 3];

then my expected output is:

>>seg.startscan 

ans=
2
ans=
4

This gave a quite similar solution, but it not vectorized and not completely right

 >>for i = 1:numel(INDS)
    seg(IND(i)).startscan=[];
 end
 >>seg.startscan 

ans=
[]
ans=
2
ans=
[]
ans=
4

Help please!

Thank you!

8
  • You can't erase a field for some of the indices. You can erase the whole records indicated by INDS, with all their fields. Does the struct array have more fields other than startscan? Commented Dec 11, 2015 at 14:21
  • Yes. It also has a seg.stopscan Commented Dec 11, 2015 at 14:24
  • 1
    You can't have for example seg(1) with only field startscan and seg(1) with fields startscan and stopscan. You need to delete all fields of seg(1). Or fill with empty: either seg(1).startscan = []; or [seg(IND).startscan] = deal([]) Commented Dec 11, 2015 at 14:25
  • 1
    @A_C: It seems you are trying something impossible. Could you put your input and your expected output in valid matlab syntax into your question? If you fail to do so, the expected output is probably not a valid data structure. Commented Dec 11, 2015 at 14:32
  • 1
    @Daniel good call on inputs/output... was a simpler problem than (i assume) we all thought Commented Dec 11, 2015 at 14:41

1 Answer 1

2

To transform

seg.startscan = [1 2 3 4 5]; INDS = [1 3];

into

seg.startscan = [2 4 5];

you simply use

seg.startscan(INDS) = [];
Sign up to request clarification or add additional context in comments.

4 Comments

That was actually my very first try. I updated the question. The error is ??? Scalar structure required for this assignment.
Sorry, I don't know what you want. Your question is very unclear. Did you paste my code as is?
Yes, and it works. But it doesn't work when I run the program. I really do not get why..
Did you forget deal()?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.