Apply a change on all the double variables of a .mat file at once

1 view (last 30 days)
Hello,
Do you have any idea on how I can apply a change on all the double variables of a .mat file at once.
The change will be a shift of 4 elements, example:
(A, B and C are variables in the .mat file)
A_before = [1 2 3 4 5]; => A_after = [0 0 0 0 1 2 3 4 5];
B_before = [11 22 33 44 55]; => B_after = [0 0 0 0 11 22 33 44 55];
C_before = [111 222 333 444 555]; => C_after = [0 0 0 0 111 222 333 444 555];
The first .mat file contains a hundred variables, and another .mat file which contains a structure of 200 variables.
How to apply this recalibration to all the variables in the .mat file at once.
Thanks

Answers (1)

Stephen23
Stephen23 on 8 May 2021
"Do you have any idea on how I can apply a change on all the double variables of a .mat file at once."
Easy:
  1. load into an output argument (which is a scalar structure)
  2. loop over the fields of that structure (hint: use fieldnames)
  3. check if the field contains a double type array
  4. make the changes you want
  5. save the structure using the -struct option
  2 Comments
Stephen23
Stephen23 on 10 May 2021
Edited: Stephen23 on 10 May 2021
"I didn't really understand on your answer what is the command that allows the change I want to do on all vectors can be done (at once)."
There is probably no one single "the command", most likely you will need multiple steps to convert multiple "vectors" to class double, something like my answer outlines. How to convert the class depends on what format/class the data currently has, which you did not describe. Possible double would help.
If you want more help with this, please upload some sample data by clicking the paperclip button.

Sign in to comment.

Categories

Find more on Historical Contests 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!