Find the 137th character in a file?

Hi, I have a file in MATLAB that has a lot of text. I would like to find the 137th character in the file.
For this I tried the following code, but witout any luck. How can any character at a given numbered position in a text file? Thanks
>> C = char(137)
C =
'‰'

8 Comments

When you use char() on numeric values, it will take the input value as a Unicode© value and output the corresponding ASCII values.
For 137, the output is an empty char, as you can observe above.
What you want to do is simply get a particular value from a data. To do that, you can read the data from the text file and use the corresponding index to obtain the value. You can use various functions for that e.g. fileread, textscan etc.
"For 137, the output is an empty char..."
No, it is a scalar character, its Unicode name is "Character Tabulation With Justification":
The fact that it is an unprintable control character does not mean that it is an "empty" character.
one = char(137)
one = '□'
isscalar(one)
ans = logical
1
isstrprop(one,'cntrl')
ans = logical
1
isempty(one) % nope, not empty.
ans = logical
0
"it will take the input value as a Unicode© value and output the corresponding ASCII values."
Not really: CHAR accepts a Unicode code point (as a decimal value) and returns a character. ASCII has nothing to do with it:
char([3734,3792,116,32,3588,350,162,105,105,32,116,275,120,116,33])
ans = 'ຖ໐t คŞ¢ii tēxt!'
Thank you for the correction @Stephen23. There was a part of brain telling me to cross-check it, but idk why I didn't do it.
Though, how did you get to print it if it's an unprintable character?
"how did you get to print it if it's an unprintable character?"
The box is displayed when the OS/browser/whatever cannot render the character.
Curiously, a few of the other control characters are not displayed with boxes:
char([0:15,137])
ans =
' □□□□□□ □□ □□□'
I see, when the code is ran the output is shown as an empty char, but when I press submit and post the response, it gets converted to these boxes as shown.
I find it weird that the live editor here shows/displays it as an empty char.
Another technical nit-pick: char casts to UTF-16. For most 'normal' characters (U+0000 to U+FFFF) this is equivalent to saying char accepts a Unicode code point, but there are exceptions (see the UTF-16 wiki page). Higher code points (e.g. emoji) need a different treatment.
char(128077)
This is incorrect. To get the correct output, you should provide the UTF-16 encoding:
char([55357 56397])
I've unaccepted the incorrect answer, and accepted the correct one i.e. @Walter Roberson's.
@Dyuman Joshi
Okay.

Sign in to comment.

 Accepted Answer

datastruct = load('data1.mat');
T = datastruct.T;
item_of_interest = T(137);

More Answers (3)

fnm = 'theNameOfYourFile.txt';
txt = fileread(fnm);
txt(137)

8 Comments

+1, The simplest approach.
Hi, thanks for the answer. It didn't work, as the other answers. Maybe because my questions was not specific enough. The original file is a .mat file, a matrix file. It is called data1.mat
When I do load data1.mat I obtain 5 visible files in the Workspace, which of one is called "T", which contains the text to be used for identification of character 137.
When I do your commands on T, I get
fnm = 'T';
txt = fileread(fnm);
Error using fileread
Could not open file T. No such file or directory.
Invalid file identifier. Use fopen to generate a valid file identifier.
I have no idea how to solve this, since these are files within a mat file.
What is the data type of T and what is its size?
It seems to be a 1x1 matrix file.
T is a char array of the size 1x5120.
Simply use
T(137)
@Sergio, you might have accepted the wrong answer.
@Dyuman Joshi: I agree. The only correct answer for a MAT file would be Walter Roberson's.

Sign in to comment.

Hassaan
Hassaan on 15 Jan 2024
Edited: Hassaan on 15 Jan 2024
Approach 1
% Open the file in read mode
fileID = fopen('yourfile.txt', 'r');
% Check if the file was opened successfully
if fileID == -1
error('File cannot be opened.');
end
% Read the entire contents of the file into a string
fileContents = fread(fileID, '*char')';
% Close the file
fclose(fileID);
% Check if the file contains at least 137 characters
if length(fileContents) >= 137
% Extract the 137th character
charAt137 = fileContents(137);
else
error('The file does not contain 137 characters.');
end
% Display the 137th character
disp(charAt137);
Make sure to replace 'yourfile.txt' with the actual name of your text file. The fread function reads the contents of the file, and the *char argument specifies that it should read the data as characters. Then, we simply index into the fileContents to find the 137th character.
Please note that this code assumes that the text file is encoded in ASCII or UTF-8 without multi-byte characters. If the text file contains multi-byte characters (like those in UTF-16 or other encodings), you will need to account for that when reading and indexing the file.
Approach 2
% Define the filename of the text file you want to read
fileName = 'FileName.txt';
% Read the entire contents of the file into a string
txtRead = fileread(fileName);
% Find and display the character at the 137th position
charAt137 = txtRead(137);
% Display the result
disp(['Character at position 137: ', charAt137]);
---------------------------------------------------------------------------------------------------------------------------------------------------------
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.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.

1 Comment

Neither of these approaches are suitable for the problem at hand.

Sign in to comment.

Shivam
Shivam on 15 Jan 2024
Edited: Shivam on 15 Jan 2024
Hi,
I understand that you want to extract the 137th character from a .txt file.
You can refer to the following workaround to extract the 137 character from a .txt file:
file = fopen('sampleTxtFile.txt', 'r');
% Check for successfully opened file
if file == -1
error('File cannot be opened.');
end
% Move to the desired position in the file (137th character)
fseek(file, 136, 'bof'); % 'bof' means beginning of file, indices start at 0
% Read one character from the current position
ch = fread(file, 1, 'char');
fclose(file);
% Convert the character code to a character
desiredCharacter = char(ch)
I hope it helps.
Thanks

5 Comments

Hi, thanks for the answer. It didn't work, as the other answers. Maybe because my questions was not specific enough. The original file is a .mat file, a matrix file. It is called data1.mat
When I do load data1.mat I obtain 5 visible files in the Workspace, which of one is called "T", which contains the text to be used for identification of character 137.
When I do your commands on T, I get
file = fopen('T', 'r');
>> fseek(file, 136, 'bof')
Error using fseek
Invalid file identifier. Use fopen to generate a valid file identifier.
I have no idea how to solve this, since these are files within a mat file.
Hi, can you attach the related files using the paperclip icon for me to debug the issue?
This is a screen shot. I attach the files too.
Hi,
Upon loading the data of 'data1.mat', 'T' obtained is a character array of size 1x5120. You can access the 137th character of T in the following way:
load data1.mat;
desiredChar = T(137)
I hope it helps.
Thanks

Sign in to comment.

Categories

Products

Release

R2023b

Tags

Asked:

on 15 Jan 2024

Commented:

on 26 Jan 2024

Community Treasure Hunt

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

Start Hunting!