MATLAB Answers

Cell returns different number of rows in a loop - How to align this?

1 view (last 30 days)
Jackson Wo
Jackson Wo on 18 Nov 2020
Commented: Jackson Wo on 19 Nov 2020
I am using a third party software command that returns a cell with X rows and 1 column (e.g. 42x1 cell). The rows contain strings that correspond to chemical phases (e.g. ETA, GAMMA_PRIME, etc.). I have set it up in MATLAB to run this computation N times, and so it will produce such a cell N times. However, on some occasions they will be slightly different in that there may be an extra row (e.g. ETA and ETA#2 might appear), therefore creating a cell with a slightly different number of rows. To illustrate this:
Run N = 1, I get 41 phases:
Run N = 2, I get 42 phases (notice everything is the same except there is a MU_PHASE#2 phase in there, shifting things down):
I am also getting numerical quantities for each of these "Phases" and so this addition/subtraction can really mess up my automated code! My question is therefore:
Is there a way to have it "check" the list on each Nth pass and align it by adding/subtracting rows so that, in the end, they all have the same number of rows? For example: Run 1 above does not have a "MU_PHASE#2" but can we add in a "MU_PHASE#2" row anyway and set it to equal 0?
I know this probably has an easy solution, but I have tried for many hours to figure out an appropriate solution but failed to do so. Any help would be greatly appreciated!

  0 Comments

Sign in to comment.

Accepted Answer

the cyclist
the cyclist on 18 Nov 2020
Something like this should help:
% Define the old and new phases info
phases_old = {'eta';'mu'};
phases_new = {'eta';'mu';'mu#2'};
% Identify missing phases from prior run
missing = setxor(phases_old,phases_new);
% Append missing to prior run
phases_old_fixed = [phases_old; missing];

  1 Comment

Jackson Wo
Jackson Wo on 19 Nov 2020
Thanks a lot! The "setxor" command is what I needed. You've saved me a lot of time.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!