Clear Filters
Clear Filters

Extract last character in table variable as new table variable

22 views (last 30 days)
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!

Accepted Answer

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

More Answers (1)

Peter Perkins
Peter Perkins on 3 May 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"

Categories

Find more on Loops and Conditional Statements 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!