在MATLAB中將資料寫入文字檔案


在MATLAB中,有一個內建函式“fprintf()”用於將資料寫入文字檔案。此函式的優點在於它可以根據指定的格式將資料寫入文字檔案。

語法

fprintf(fileID, FormateSpecifier, Variables);

MATLAB中的格式說明符

下表列出了MATLAB中“fprintf()”函式使用的不同格式說明符

序號 格式說明符 描述
1. %d 或 %i 將值顯示為整數。
2. %f 顯示浮點數。
3. %s 顯示字串陣列或字元向量。
4. %e 以指數形式顯示值。
5. %c 顯示單個字元。
6. %g 顯示沒有尾隨零的值。

MATLAB中的轉義序列

下表列出了MATLAB中“fprintf()”函式使用的不同轉義序列

序號 轉義序列 描述
1.
建立新行。
2. \t 插入水平製表符。
3. \v 插入垂直製表符。
4. \r 回車。
5. \ 反斜槓
6. \b 退格。
7. \f 換頁。
8. %% 百分號字元
9. \a 報警
10. '' 單引號

演算法

步驟1 - 將您的資料儲存在變數或變數中。

步驟2 - 開啟一個檔案以將資料寫入其中。為此,請使用“fopen()”函式。

步驟3 - 檢查檔案是否已成功開啟。如果不是,則顯示錯誤訊息。為此,請將檔案識別符號與-1進行比較。

步驟4 - 將資料寫入文字檔案。為此,請使用“fprintf()”函式。

步驟5 - 關閉檔案以釋放系統資源。為此,請使用“fclose()”函式。

步驟6 - 向用戶顯示程序完成訊息。

現在,讓我們通過幾個示例程式來了解在MATLAB中將資料寫入文字檔案的過程。

示例

% A MATLAB program to write multiple numeric values to a 
text file.
% Create a sample array of integers. 
x = [10 20 30 40 50; 100, 200, 300, 400, 500; 1000, 2000, 
3000, 4000, 5000];
% Open the file for writing
file1 = fopen('TextFile1.txt', 'w');
% Check if the file was successfully opened
if file1 == -1
   error('Failed to open the file.');
end
% Write data to the text file
   fprintf(file1, '%d %d %d 
', x); % Close the file to free up system resources fclose(file1); disp('Data has been written to the text file.');

輸出

Data has been written to the text file.

透過編譯以下語句檢視文字檔案的內容

disp('Data in the File:');
type TextFile1.txt;

解釋

在這個MATLAB程式中,我們建立了一個包含整數的陣列“x”。我們想將此資料寫入名為“TextFile1.txt”的文字檔案。

為此,我們首先使用“w”作為訪問模式呼叫“fopen”函式,以寫入模式開啟檔案。為了引用開啟的檔案,我們使用檔案識別符號“file1”。然後,我們檢查檔案是否成功開啟,如果檔案已開啟,則顯示錯誤訊息。之後,我們使用“%d”作為格式說明符呼叫“fprintf”函式,以將整數資料寫入文字檔案。然後,我們呼叫“fclose”函式關閉檔案以釋放資源。最後,我們顯示一條訊息,通知資料已成功寫入檔案。

此外,要檢視寫入檔案的資料,我們執行語句“type TextFile1.txt”。

示例

% A MATLAB program to write floating point numeric values to a text 
file.
% Create a sample array of floating point numbers. 
x = [10.25 20.52 30.56 40.45 50.65; 100.552, 200.235, 300.563, 
400.456, 500.256; 1000.4256, 2000.2356, 3000.2356, 4000.2563, 
5000.2356];
% Open the file for writing
file2 = fopen('FloatingPoint.txt', 'w');
% Check if the file was successfully opened
if file2 == -1
   error('Failed to open the file.');
end
% Write data to the text file
   fprintf(file2, '%f %f %f 
', x); % Close the file to free up system resources fclose(file2); disp('Data has been written to the text file.');

輸出

Data has been written to the text file.

透過編譯以下語句檢視文字檔案的內容

disp('Floating Point Numbers in the File:');
type FloatingPoint.txt;

解釋

在這個MATLAB程式中,我們建立了一個包含浮點數的陣列“x”。我們想將此資料寫入名為“FloatingPoint.txt”的文字檔案。

為此,我們使用“w”作為訪問模式呼叫“fopen”函式,以寫入模式開啟檔案。為了引用開啟的檔案,我們使用檔案識別符號“file2”。然後,我們檢查檔案是否成功開啟,如果檔案已開啟,則顯示錯誤訊息。之後,我們使用“%f”作為格式說明符呼叫“fprintf”函式,以將資料寫入文字檔案。然後,我們呼叫“fclose”函式關閉檔案以釋放資源。最後,我們顯示一條訊息,通知資料已成功寫入檔案。

要檢視寫入檔案的資料,我們執行語句“type FloatingPoint.txt”。

示例

% A MATLAB program to write a string of text to a text file.
% Create a sample string of text. 
x = 'Tutorials Point is a Best Online Platform to Learn Coding.';
% Open the file for writing
file3 = fopen('MyFile.txt', 'w');
% Check if the file was successfully opened
if file3 == -1
   error('Failed to open the file.');
end
% Write data to the text file
   fprintf(file3, '%s', x);
% Close the file to free up system resources
fclose(file3);
disp('Data has been written to the text file.');

輸出

Data has been written to the text file.

透過編譯以下語句檢視文字檔案的內容

disp('String in the text file is ');
type MyFile.txt;

示例

% A MATLAB program to write data to a text file using a 
mathematical expression.
% Create sample numeric data. 
x = 1:10;
% Specify a mathematical expression
y = [x; x.^3];
% Open the file for writing
file4 = fopen('cubepower.txt', 'w');
% Check if the file was successfully opened
if file4 == -1
   error('Failed to open the file.');
end
% Write data to the text file
   fprintf(file4, '%s \t \t %s 
', 'x', 'cube'); fprintf(file4, '%f \t %f
', y); % Close the file to free up system resources fclose(file4); disp('Data has been written to the text file.');

輸出

執行以下程式碼以獲取結果

type cubepower.txt;

示例

% A MATLAB program to write a hyperlink to a text file.
% Specify the website url.
url = 'https://tutorialspoint.tw/index.htm';
% Specify the name of the website
website = 'Tutorials Point';
% Open the file for writing
file5 = fopen('tutorialspoint.txt', 'w');
% Check if the file was successfully opened
if file5 == -1
   error('Failed to open the file.');
end
% Write the hyperlink to the text file
   fprintf(file5, ' "%s"  
', url, website); % Close the file to free up system resources fclose(file5); disp('Data has been written to the text file.');

輸出

執行以下程式碼以獲取結果

type tutorialspoint.txt;

結論

這就是在MATLAB中將資料寫入文字檔案的方法。上述MATLAB程式說明了在MATLAB中使用“fprintf()”函式將資料寫入文字檔案的實現。

更新於:2023年7月18日

407 次瀏覽

啟動您的職業生涯

完成課程獲得認證

開始
廣告
© . All rights reserved.