필터 지우기
필터 지우기

Find the 137th character in a file?

조회 수: 7 (최근 30일)
Sergio
Sergio 2024년 1월 15일
댓글: Hassaan 2024년 1월 26일
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
Dyuman Joshi
Dyuman Joshi 2024년 1월 24일
I've unaccepted the incorrect answer, and accepted the correct one i.e. @Walter Roberson's.
Hassaan
Hassaan 2024년 1월 26일
@Dyuman Joshi
Okay.

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

채택된 답변

Walter Roberson
Walter Roberson 2024년 1월 16일
datastruct = load('data1.mat');
T = datastruct.T;
item_of_interest = T(137);

추가 답변 (3개)

Stephen23
Stephen23 2024년 1월 15일
fnm = 'theNameOfYourFile.txt';
txt = fileread(fnm);
txt(137)
  댓글 수: 8
Dyuman Joshi
Dyuman Joshi 2024년 1월 18일
@Sergio, you might have accepted the wrong answer.
Stephen23
Stephen23 2024년 1월 18일
@Dyuman Joshi: I agree. The only correct answer for a MAT file would be Walter Roberson's.

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


Hassaan
Hassaan 2024년 1월 15일
편집: Hassaan 2024년 1월 15일
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
Walter Roberson
Walter Roberson 2024년 1월 18일
Neither of these approaches are suitable for the problem at hand.

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


Shivam
Shivam 2024년 1월 15일
편집: Shivam 2024년 1월 15일
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
Shivam
Shivam 2024년 1월 17일
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
Sergio
Sergio 2024년 1월 17일
Thanks!

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

카테고리

Help CenterFile Exchange에서 Low-Level File I/O에 대해 자세히 알아보기

태그

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by