Finding strings with common character
Mostra commenti meno recenti
I would like to find in a text file all unique strings with common first character, e.g. "G" (unique i.e. without repetition: if any, the same, string occurs several tims I need to specify/print it only once.
Any help would be appreciated.
1 Commento
madhan ravi
il 22 Dic 2023
Modificato: madhan ravi
il 22 Dic 2023
Give an example or attach your text file and show the expected result.
Risposta accettata
Più risposte (3)
Read the data into MATLAB, split it into separate words if necessary, then use startsWith to determine which words start with your desired character.
L = readlines('bench.dat');
oneLine = L(1) % Just operate on the first line
s = split(oneLine)
startsWithB = startsWith(s, "B")
wordStartingWithB = s(startsWithB)
The unique function likely will be useful to you as well.
To achieve this in MATLAB, you would typically read the text file into a string array or cell array, then use string manipulation functions to find and list the unique strings. Here's a step-by-step guide with code snippets:
Read the Text File: Load the contents of the text file into MATLAB.
filename = 'yourfile.txt'; % Replace with your text file name
fileID = fopen(filename, 'r');
data = textscan(fileID, '%s');
fclose(fileID);
extractedStrings = data{1};
Filter Strings by First Character: Find strings that start with the specified character.
commonChar = 'G'; % Replace with the common character you're looking for
startsWithG = strncmp(extractedStrings, commonChar, 1);
filteredStrings = extractedStrings(startsWithG);
Find Unique Strings: Get the unique strings from the filtered list.
uniqueStrings = unique(filteredStrings);
Print Unique Strings: Display or print the unique strings.
disp(uniqueStrings);
On MATLAB, you can run this script after replacing 'yourfile.txt' with the actual path to your text file and commonChar with the character you're interested in. This will print all unique strings that start with that character, displaying each string only once.
Full Code:
% Specify the file name and the common character
filename = 'yourfile.txt'; % Replace with your text file name
commonChar = 'G'; % Replace with the common character you're looking for
% Open the text file for reading
fileID = fopen(filename, 'r');
if fileID == -1
error('File not found or permission denied');
end
% Read the content of the file into a cell array of strings
data = textscan(fileID, '%s');
fclose(fileID); % Close the file after reading
extractedStrings = data{1}; % Extract the strings from the cell array
% Filter strings by the first character
startsWithCommonChar = strncmp(extractedStrings, commonChar, 1);
% Get the unique strings that start with the specified character
filteredStrings = extractedStrings(startsWithCommonChar);
uniqueStrings = unique(filteredStrings);
% Print the unique strings
disp('Unique strings starting with the specified character:');
disp(uniqueStrings);
Input file content:
Gabc
abcde
G123
Output:
Unique strings starting with the specified character:
{'G123'}
{'Gabc'}
For instance, if you need the output as a simple list without the curly braces and single quotes, you can loop through the cell array and print each string:
disp('Unique strings starting with the character G:');
for i = 1:length(uniqueStrings)
disp(uniqueStrings{i});
end
Input file content:
Gabc
abcde
G123
Output:
Unique strings starting with the specified character:
G123
Gabc
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
4 Commenti
Dyuman Joshi
il 22 Dic 2023
Modificato: Dyuman Joshi
il 22 Dic 2023
What if the data read has no spaces in between? or separate by another delimeter?
Also, strings is a built-in function. It's not a good idea to use functions as variable (or script) names.
Dyuman Joshi
il 22 Dic 2023
You've only updated for the 2nd point I raised.
Say the input is -
G123Gabc
Yo123G321Yo
What should be the output then?
type Gfile.txt
% assuming strings to return are space delimited
text = split(string(fileread('Gfile.txt')));
unique(text(startsWith(text,"G")))
Categorie
Scopri di più su Characters and Strings in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!