Extract last character in table variable as new table variable

36 visualizaciones (últimos 30 días)
Hi,
I'm trying to extract the last character of a variable in a table (tAll.Sym) to a new varaible, but cant figure out the syntax. I was able to get the first two characters to a new variable tAll.Product using:
tAll.Product = strtrim(extractBetween(tAll.Sym,1,2));
but I cant determine how to extract only the last character, it seems that you cannot use 'end', and my "Sym" variables are not all the same length so i cannot hard code 10 as the last.
Obviously I can create a loop and iterate over the whole table, but i thought i should be able to do it without a loop like the code above. Thanks for any help in advance!

Respuesta aceptada

Walter Roberson
Walter Roberson el 30 de Abr. de 2019
Last = @(v) v(end);
tAll.Product = arrayfun(@(S) Last(char(S)), tAll.Sym);
This uses a hidden loop instead of an explicit loop.

Más respuestas (1)

Peter Perkins
Peter Perkins el 3 de Mayo de 2019
As Walter showed, this is more of a "string" question than a "table" question. In MATLAB versions since (IIRC) R2016b-ish, try this alternative to arrayfun:
>> s = ["ax"; "bby"; "cccz"]
s =
3×1 string array
"ax"
"bby"
"cccz"
>> extractAfter(s,strlength(s)-1)
ans =
3×1 string array
"x"
"y"
"z"

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by