2013-08-24 95 views
2

你能告訴我怎樣才能從matlab中的用戶獲得多個輸入?我想直接獲得一個數組,但這似乎不可能。我嘗試了以下方法多輸入matlab

 velocity = input('Enter the velocities you want the aircraft to have at every node with space in between(m/s)','s'); 

和更高版本使用分隔符來讀取空格之間的數字。但即使如此,我不確定如何使用內置功能。

 [u,remain] = strtok(velocity); 

如果沒有辦法直接獲取多個輸入,我怎麼可以把上面的一個循環,這樣我可以閱讀所有的數字嗎?我很抱歉,如果問題是非常基本的,你的幫助將不勝感激。

回答

3

給數組作爲輸入

>> velocity = input('Enter the velocities you want the aircraft to have at every node with space in between(m/s)','s'); 
Enter the velocities you want the aircraft to have at every node with space in between(m/s) [1 2 3] 
>> velocity 

velocity = 

[1 2 3] 

然後可以使用velocity(1), velocity(2), ...

或者,如果你計劃給使用表達式作爲逗號分隔輸入

>> velocity = input('Enter the velocities you want the aircraft to have at every node with space in between(m/s)','s'); 
Enter the velocities you want the aircraft to have at every node with space in between(m/s)1,2,3 
>> result=regexp(velocity,',','split') 

result = 

    '1' '2' '3' 

(同樣可以使用用於分隔輸入的空間)

+0

感謝您的快速響應。 :) –

+0

@LakshmiNarayanan:D – P0W

+1

我覺得有一個小錯誤。第一個,不直接給陣列。您必須在輸入參數的末尾刪除's'參數。 –

1

這可以通過:

result = input('prompt'); 

Matlab會提示輸入你的'提示',你可以輸入例如[1 2 3]。結果將與其中的前一個數字一起構成向量。