Substitute and add rows to a cella rray
Mostra commenti meno recenti
Hi everyone,
I have two cells of strings: A(a,1) and B(b,1), where a>b.
I want to add B in A at a certain position, where there are already c blank rows (c<b).
I tried it with "replace", obtaining the following error:
"Replacement text must be either scalar or the same size as the match text."
Any suggestion?
Thank you :)
3 Commenti
dpb
il 4 Gen 2022
"I want to add B in A at a certain position, where there are already c blank rows (c<b)."
So, what happens to the excess elements of B(b+1:c)? Is A overwritten, B truncated to only copy c elements or is A to grow to allow the insertion?
Francesca Danielli
il 5 Gen 2022
KSSV
il 5 Gen 2022
It would be better if you show us a small example code with data.
Risposte (1)
SANKALP DEV
il 13 Set 2023
Hi Francesca,
I understand that you are currently facing a challenge involving the addition of a cell array 'B' into specific positions within 'A,' where there are existing 'C' blank rows.
The error message you've encountered suggests that the replacement text provided during the "replace" operation may not align in size with the matched text.
To address this error, a solution would involve splitting the cell array 'A' into two parts
- The substring before the positions of the blank rows.
- The substring after the positions of the blank rows.
Following this split, you can concatenate the string preceding the blank, the contents of cell array 'B,' and the string following the blank. Finally, assign this updated array back to 'A.'
I have assumed that you already know the index of the blank rows in 'A.' However, if you're uncertain about these indices, you can find them using the following code:
blank_positions = find(cellfun('isempty', A));
For more detailed information about the functions mentioned above, you can refer to the following documentations:
- https://www.mathworks.com/help/releases/R2020b/matlab/ref/cellfun.html?searchHighlight=cellfun&s_tid=doc_srchtitle
- https://www.mathworks.com/help/releases/R2020b/matlab/ref/find.html?searchHighlight=find&s_tid=doc_srchtitle
Hope this helps.
Categorie
Scopri di più su Entering Commands in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!