如何在MATLAB中反向讀取文字檔案?
在MATLAB中,反向讀取文字檔案涉及反轉檔案中寫入的行的順序。我們可以使用MATLAB反轉順序並反向讀取文字檔案。為此,MATLAB提供了使用不同函式和演算法的各種方法。在本教程中,我將解釋使用MATLAB反向讀取文字檔案的常用方法。
在MATLAB中建立文字檔案
在MATLAB中,我們可以使用內建函式“fopen”和“fprintf”建立文字檔案。
示例
以下示例演示了在MATLAB中建立文字的過程。
% MATLAB code to create a text file % Open a text file in writing mode file_id = fopen('TextFile.txt', 'w'); % Check whether the file is opened successfully if file_id == -1 fprintf('Unable to open or create the file.'); return; end % Specify the content to be written to the text file content = "Tutorials Point is an e-Learning platform designed to provide high-quality content on various subjects."; content = [content, "You can learn from video tutorials."]; content = [content, "You can learn from text tutorials."]; % Write the content to the text file fprintf(file_id, '%s
', content); % Close the text file fclose(file_id); % Display a confirmation message fprintf(' The "TextFile.txt" has been created successfully.');
輸出
The "TextFile.txt" has been created successfully.
內容 −
Tutorials Point is an e-Learning platform designed to provide high-quality content on various subjects. You can learn from video tutorials. You can learn from text tutorials.
現在讓我們討論使用MATLAB反向讀取文字檔案的不同方法。
方法1:逐字元反向讀取文字檔案
此方法涉及以下步驟以反向讀取文字檔案:
步驟(1) − 以讀取模式開啟您的文字檔案。為此,請使用帶有“r”引數的“fopen”函式。
步驟(2) − 建立一個空字串來儲存反轉的文字。
步驟(3) − 透過將檔案指標移動到檔案的末尾來確定檔案的總大小。為此,請使用帶有“eof”的“fseek”函式,其中“eof”代表“檔案結尾”。
步驟(4) − 使用“ftell”函式確定文字檔案的總大小(以位元組為單位)。
步驟(5) − 迭代迴圈遍歷檔案的內容以反向讀取。
步驟(6) − 關閉檔案以釋放系統資源。
步驟(7) − 顯示結果。
這就是此方法中用於在MATLAB中反向讀取文字檔案的演算法。
示例
讓我們來看一個例子來理解程式碼實現演算法。
% MATLAB code to read a text file backward % Open the text file in read mode file_id = fopen('TextFile.txt', 'r'); % Check whether the file is opened successfully if file_id == -1 fprintf('Unable to open the text file.'); return; end % Create an empty string to store the backward text back_text = ''; % Move the file pointer to end of the text file fseek(file_id, 0, 'eof'); % Determine the total file size file_size = ftell(file_id); % Iterate a loop to read the characters in the file in backward direction for p = file_size : -1 : 1 % Determine current position of file pointer fseek(file_id, p, 'bof'); % Move the file pointer to current position c = fread(file_id, 1, 'char'); % Read the character at current position back_text = [back_text, c]; % Store the character in the backward text end % Close the text file fclose(file_id); % Display the backward text disp(back_text);
輸出
.slairotut txet morf nrael nac uoY .slairotut oediv morf nrael nac uoY .stcejbus suoirav no tnetnoc ytilauq-hgih edivorp ot dengised mroftalp gninraeL-e na si tnioP slairotu
解釋
從輸出中可以看出,在此方法中,我們已反向讀取文字檔案。在此方法中,檔案是逐字元反向讀取的。
讓我們討論另一種在MATLAB中反向讀取文字檔案的方法。
方法2:逐行反向讀取文字檔案
下面解釋了此方法中在MATLAB中反向讀取文字檔案的步驟。
步驟(1) − 使用“r”模式使用“fopen”函式開啟文字檔案。
步驟(2) − 檢查檔案是否成功開啟。
步驟(3) − 建立一個空元胞陣列以反向順序儲存文字行。
步驟(4) − 定義一個迴圈來讀取檔案中的文字行,並以反向順序將它們儲存在元胞陣列中。
步驟(5) − 使用“fclose”函式關閉文字檔案。
步驟(6) − 以反向順序顯示每一行的文字。
示例
讓我們來看一個例子來實現這些步驟以反向讀取文字檔案。
% MATLAB code to read a text file backwards % Open the text file in read mode file_id = fopen('TextFile.txt', 'r'); % Check whether the file is opened successfully if file_id == -1 fprintf('Unable to open the file.'); return; end % Create an empty cell array to store lines of text in backward order back_lines = {}; % Define a loop to read text lines in the file and store in the cell array while true l = fgetl(file_id); % Read the current text line % Break the loop if we have reached end of the file if ~ischar(l) break; end % Store the line to the cell array in backward order back_lines = [l; back_lines]; end % Close the text file fclose(file_id); % Display the lines in backward order for i = 1 : length(back_lines) disp(back_lines{i}); end
輸出
You can learn from text tutorials. You can learn from video tutorials. Tutorials Point is an e-Learning platform designed to provide high-quality content on various subjects.
解釋
很明顯,此方法中使用的演算法逐行反向讀取文字檔案。
結論
在本教程中,我解釋了兩種使用MATLAB以反向順序讀取文字檔案的簡單方法。在某些特定應用程式中,反向讀取文字檔案有時至關重要。我們可以使用MATLAB反向讀取文字檔案。我透過示例演示了逐步過程,以使用MATLAB反向讀取文字檔案。
第一種方法逐字元反向讀取文字檔案,而第二種方法逐行反向讀取文字檔案。您可以嘗試使用您自己的文字檔案使用這些方法。