Filter löschen
Filter löschen

Extract last character in table variable as new table variable

22 Ansichten (letzte 30 Tage)
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!

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 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.

Weitere Antworten (1)

Peter Perkins
Peter Perkins am 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"

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by