sorting string based on the value in it

'temp_25_vgs_4',
'temp_25_vgs_5' ,
'temp_25_vgs_8' ,
'temp_25_vgs_6' ,
'temp_25_vgs_9',
'temp_25_vgs_7'
how do i sort the above in the increasing order of vgs.
how to sort the above kind of strings..i need it in order (
'temp_25_vgs_4'
'temp_25_vgs_5'
'temp_25_vgs_6'
'temp_25_vgs_7'
'temp_25_vgs_8'
'temp_25_vgs_9'
)

댓글 수: 4

Sajid Afaque
Sajid Afaque 2020년 1월 8일
is their anyway where we can do without using these functions
Stephen23
Stephen23 2020년 1월 8일
"is their anyway where we can do without using these functions"
Of course: parse the string to split/extract all relevant characters and numbers (e.g. based on a regular expression or isstrprop), convert the numbers to numeric, sort. You can search this forum to find examples of this, the search field is at the top of the page.

댓글을 달려면 로그인하십시오.

답변 (1개)

Arjun
Arjun 2024년 8월 6일

0 개 추천

Hi,
As per my understanding of the question you want to custom sort a list of strings without using inbuilt functions. I can give you a basic workflow of how to proceed and some dummy code which you can use to construct solution for your application.
Steps:
  1. Preprocess the string.
  2. Manually extract the numeric part from the string after the last _(underscore).
  3. Construct the numeric value.
  4. Write custom sort function such as selection sort or bubble sort to sort the list of strings based on the numeric value extracted.
Here is some dummy code which might be helpful in better understanding of the solution.
% Define the list of variable names
variable_names = {
'temp_25_vgs_44',
'temp_25_vgs_5',
'temp_25_vgs_812',
'temp_25_vgs_6',
'temp_25_vgs_99',
'temp_25_vgs_7'
};
% Function to extract numeric part
function num = extract_vgs_value(str)
% Find the position of the last underscore
underscore_pos = find(str == '_', 1, 'last');
% Extract the numeric part after the last underscore
num_str = str(underscore_pos+1:end);
% Convert to number
num = 0;
for i = 1:length(num_str)
num = num * 10 + (num_str(i) - '0');
end
end
% Implement bubble sort to sort the variable names based on numeric values
n = length(variable_names);
for i = 1:n-1
for j = 1:n-i
if extract_vgs_value(variable_names{j}) > extract_vgs_value(variable_names{j+1})
% Swap the variable names
temp = variable_names{j};
variable_names{j} = variable_names{j+1};
variable_names{j+1} = temp;
end
end
end
% Display the sorted list of variable names
disp('Sorted list of variable names:');
disp(variable_names);
In the code provided above we have used find function from MATLAB whose documentation you can refer in the attached link below.

댓글 수: 1

"Implement bubble sort to sort the variable names based on numeric values"
MATLAB already has a very efficient inbuilt SORT function.
Simpler and more efficient:
C = {'temp_25_vgs_44';'temp_25_vgs_5';'temp_25_vgs_812';'temp_25_vgs_6';'temp_25_vgs_99';'temp_25_vgs_7'}
C = 6x1 cell array
{'temp_25_vgs_44' } {'temp_25_vgs_5' } {'temp_25_vgs_812'} {'temp_25_vgs_6' } {'temp_25_vgs_99' } {'temp_25_vgs_7' }
[~,X] = sort(str2double(extract(C,digitsPattern+textBoundary)));
D = C(X)
D = 6x1 cell array
{'temp_25_vgs_5' } {'temp_25_vgs_6' } {'temp_25_vgs_7' } {'temp_25_vgs_44' } {'temp_25_vgs_99' } {'temp_25_vgs_812'}

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

제품

태그

질문:

2020년 1월 8일

편집:

2024년 8월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by