Info
此问题已关闭。 请重新打开它进行编辑或回答。
sort a string based on particular value
1 次查看(过去 30 天)
显示 更早的评论
Hello All,
i have a list of type
ch = { 'temp_25_vgs_8', 'temp_25_vgs_3', 'temp_25_vgs_9', 'temp_25_vgs_10', 'temp_20_vgs_8', 'temp_20_vgs_5', 'temp_25_vgs_5'};
i need to sort it based on vgs value (or both vgs and temp) and get back the list as
ch = { 'temp_20_vgs_5',
'temp_20_vgs_8',
'temp_25_vgs_3',
'temp_25_vgs_5',
'temp_25_vgs_8',
'temp_25_vgs_9',
'temp_25_vgs_10'
} ;
please help me resolve that,
than you in advance
0 个评论
回答(1 个)
Sriram Tadavarty
2020-3-19
编辑:Sriram Tadavarty
2020-3-19
Hi Sajid,
chs = arrayfun(@(x) str2double(string([ch{x}(5+1:8-1) ch{x}(13:end)])),1:numel(ch))
[~,i] = sort(chs);
out = ch(i);
Hope this helps.
Regards,
Sriram
2 个评论
Sriram Tadavarty
2020-3-19
Hi Sajid,
Here is what you are looking for, if you know what the index locations are already, then you perform something as such:
chs = arrayfun(@(x) str2double(string([ch{x}(5+1:8-1) ch{x}(13:end)])),1:numel(ch))
[~,i] = sort(chs);
out = ch(i);
To find the locations exactly which has the starting numbers, you can use this
ind = regexp(ch,'_'); % it provides index in each cell
Let me know if this helped.
Thanking you.
Regards,
Sriram
此问题已关闭。
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!