Reodering/Swapping Arrays in a struct

Hello Everyone, I am new to matlab. I am having a problem which is reodering the arrays in a structure by taking in user input.
What I mean is user will enter some values in a vector and according to them, arrays in the struct should be reodered.
As you can see in the pictures, the vector given by the user is 1x7 and the struct is 7x1 and I want to reoder the arrays in the struct according to the vector.
My vector is named as "a" and the struct is named as "out"
Now my code is:
for i = 1:7
a(i) = input('Input a number: ');
if (a(i) > 7)
{ error('Value exceeds the Number of Struct Rows')
}
end
end
for i =1:7
out(a(i)) == out(i)
end
this is a vector givien by the user
this is the struct, which I have
The error I am getting is
"Undefined operator '==' for input arguments of type 'struct'."
I donnot know how to resolve it or what is wrong in my code

 Réponse acceptée

Guillaume
Guillaume le 19 Oct 2019
You seem to be writing matlab code using C syntax. In matlab if conditions don't need to be enclosed in () brackets
if a(i) > 7
and more importantly the body of if must not be enclosed in {}
{ error('Value exceeds the Number of Struct Rows')
}
attempts to create a cell array containing the output of error and since error doesn't have any output, the actual error message produced by the above would be: Error using error. Too many output arguments.
So, that part should be:
if a(i) > 7
error('Value exceeds the Number of Struct Rows');
end
or more simply
assert(a(i) <= 7, 'Value exceeds the Number of Struct Rows')
Even better get rid of the loop:
a = input('Enter a vector of seven numbers using [1 2 3 4 5 6 7] syntax:');
assert(all(a <= 7), 'At least one value exceeds the number of rows');
In C and in matlab == is the comparison operator, not the assignment. Matlab doesn't know how to compare structure.
Once again, a loop is not needed:
out(a) = out; %reorders the elements of out.
Of course, since you never check that all the elements of a are different, you may well end up with duplicated elements in your reordered structure (and others missing).

8 commentaires

Chris Dan
Chris Dan le 19 Oct 2019
Modifié(e) : Chris Dan le 19 Oct 2019
Hey Thanks for the answer. I am getting this problem, some elements are missing and some are changed and otehrs got missing, can you tell me how do I check that all elements of "a" are different?
assert(numel(unique(a)) == numel(a), 'Some elements are duplicated')
You might also want to check that the numbers are valid indices (i.e > 0 and integers)
Chris Dan
Chris Dan le 19 Oct 2019
Hey, Thanks for the reply!! :) I have edited the code and now I am using it but I am still having elements repeated and changed. I am attaching a picture. This is the result I am getting
julian4.JPG
Where as, I should be getting is
2x10
2x11
3x10
2x10
2x10
2x11
4x10
I dont know why this is still happening, I would be glad if you could help me!
Guillaume
Guillaume le 19 Oct 2019
What is the content of a?
Chris Dan
Chris Dan le 19 Oct 2019
a is "2,4,7,6,5,3,1"
This is the vector given by the user and according to this, reodering is not getting properly done, some elements are repeating and some are missing...
If you're still using your loop to reorder the array, then yes the reordering will not be getting done properly at all.
If you use the simple one-liner I gave you, the ordering will be correct:
out(a) = out;
The loop you wrote doesn't work properly at all (even if your replace == by =) and is easily demonstrated with your a = [2 4 7 6 5 3 1].
Assuming we start with out = [orig1, orig2, orig3, orig4, ...]
Step 1: i = 1. Your code does
out(2) = out(1) %a(i) is a(1) is 2, i is 1
%out(1) is origi1, so copy orig1 in out(2)
as a result, out is now [orig1, orig1, orig3, orig4, ...]. Note that you've lost orig2 forever.
Step 2: i = 2. Your code does
out(4) = out(2) %a(i) is a(2) is 4, i is 2
%out(2) is orig1 since that's what we put there in the previous step. copy orig1 in out(4)
as a result, out is now [orig1, orig1, orig3, orig1, ...]. Note that you've now lost orig4 forever and now have 3 copies of orig1.
and so on...
Chris Dan
Chris Dan le 19 Oct 2019
Modifié(e) : Chris Dan le 19 Oct 2019
Hey, I am using the one line u send me and still gettin the same results.
some of the elements get missing and some get duplicated
here is my code:
% take input from the user to reoder the files in the struct
for i = 1:7
a(i) = input('Input a number: ');
assert(a(i) <= 7, 'Value exceeds the Number of Struct Rows')
assert(numel(unique(a)) == numel(a), 'Some elements are duplicated')
end
% transpose the vector to match the dimension of the struct
b = transpose(a);
%reorders the elements of out.
out(b) = out;
Chris Dan
Chris Dan le 19 Oct 2019
Modifié(e) : Chris Dan le 19 Oct 2019
I think I have solved it! :)
I ust fliped the condition
Now the code is
%reorders the elements of out.
out = out(b);
I want to Thank you for helping me in my problem :) :) :)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by