天天看點

matlab 生成一個檔案夾,MATLAB建立和通路檔案

最簡單的方式是使用一個特定類型的二進制檔案,稱為MAT檔案(MAT file)​

這是MATLAB内部檔案,save和load即可​

舉例如下:​

g=9.81, m=80;t=5;

>> cd=[.25 .267 .245 .28

.273]'

cd =

0.2500

0.2670

0.2450

0.2800

0.2730

>>

v=sqrt(g*m/cd)*tanh(sqrt(g*cd/m)*t)

v =

38.5921

>> save veldrag v cd

>> clear

>> v

??? Undefined function or variable 'v'.

>> load veldrag

>> who

Your variables are:

cd  v

從中可以很好地看出save和load的作用​

但是當MATLAB與其他程式進行互動時,還是需要其他的方法​

學習以通用的ASCII格式建立文本檔案​

格式:save

-ascii

用MAT檔案可能向儲存整個工作空間,而ASCII檔案通常用于儲存一個矩陣的值​​

舉例如下:​​

>> A=[5 7

9  2; 3  6 3 9]

A =

5

7

9

2

3

6

3

9

>> save simpmatrix.txt

-ascii>> cd

cd =

0.2500

0.2670

0.2450

0.2800

0.2730

>> v

v =

38.5921

>> clear

清空之後工作空間中所有的變量全部都被清除了

>> v

??? Undefined function or variable

'v'. >> cd

C:\Users\paper\Desktop

>> A???

Undefined function or variable 'A'.

>> load simpmatrix.txt???

Error using ==> 原因是在儲存矩陣的同時也儲存上面的cd

和v的值,而他們的格式不同,是以無法讀取

loadNumber of columns on line 2 of ASCII file

C:\Users\paper\Desktop\simpmatrix.txtmust be the same as previous

lines.

>> load simpmatrix.txt

??? Error using ==> loadNumber of columns on line

2 of ASCII file C:\Users\paper\Desktop\simpmatrix.txtmust be the

same as previous lines.​

我手動将文本檔案裡的内容删除後,發現格式相同,讀取成功

>> load

simpmatrix.txt>> simpmatrix

simpmatrix =

5

7

9

2

3

6

3

9​

再換一種方式,先手動将txt文本删除,然後實驗驗證​

>> simpmatrix

simpmatrix =

5

7

9

2

3

6

3

9

>> load simpmatrix.txt

??? Error using ==> loadUnable to read file

simpmatrix.txt: No such file or directory.

首先确認檔案的确不存在

>> save simpmatrix.txt -ascii

工作空間中的變量依然存在,需要清除​

>> simpmatrix

simpmatrix =

5

7

9

2

3

6

3

9

>> clear

>> simpmatrix

??? Undefined function or variable

'simpmatrix'.

>> A=load(simpmatrix.txt)

??? Undefined function or variable

'simpmatrix'.

>> A=load(simpmatrix.txt)

??? Undefined function or variable

'simpmatrix'. ​

load幾次均不成功,發現解決方案如下

>> A=load('simpmatrix.txt')

A =

5

7

9

2

3

6

3

9