Effacer les filtres
Effacer les filtres

Extract last character in table variable as new table variable

34 vues (au cours des 30 derniers jours)
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!

Réponse acceptée

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

Plus de réponses (1)

Peter Perkins
Peter Perkins le 3 Mai 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"

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by