Extract last character in table variable as new table variable

21 次查看(过去 30 天)
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!

采纳的回答

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

更多回答(1 个)

Peter Perkins
Peter Perkins 2019-5-3
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"

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by