天天看點

figure在Matlab函數檔案中的作用

figure在腳本中是新開一個畫圖視窗,在腳本中即使沒有figure語句,也能進行畫圖。然而,當腳本檔案有plot語句,函數檔案中也有plot語句,假如函數檔案中沒有figure語句的話,就無法進行畫圖了。

舉例1:

腳本檔案代碼:

test();
           

函數檔案test.m代碼:

function  test()
plot([1 2 3 4],[2 3 4 5])
end
           

運作後,能進行正常的畫圖。

但是,如果對代碼進行修改,使函數檔案有傳回值,那麼沒有figure語句将不能進行畫圖。

舉例2:

腳本檔案代碼:

test();
plot([1 4 6 1],[-1 -2 -4 -1])
           

函數檔案test.m代碼:

function  test()
plot([1 2 3 4],[1 2 3 4])
end
           

運作後,發現隻有一幅圖,函數檔案中的plot([1 2 3 4],[1 2 3 4])沒起作用。

figure在Matlab函數檔案中的作用

當我在腳本檔案和函數檔案中均加入figure語句後,函數檔案中的plot語句就起作用了。

舉例3:

腳本檔案代碼:

test();
figure
plot([1 4 6 1],[-1 -2 -4 -1])
           

函數檔案test.m代碼:

function  test()
figure
plot([1 2 3 4],[1 2 3 4])
end
           

結論:為了避免不必要的困擾,每畫一幅圖前均加上figure語句吧。

繼續閱讀