2012-02-15 27 views
0

我正在整理我的代碼。我有一個包含5列的數組,每個列都被分配給一個變量。目前,我使用:MATLAB:根據行數組的內容設置很多變量

x = inputData(i,1); 
y = inputData(i,2); 
currentSampleTime = inputData(i,3); 
velocityX = inputData(i,4); 
velocityY = inputData(i,5); 

我以爲我可以只是做以下收拾了一點東西:

[x y currentSampleTime velocityX velocityY] = inputData(i,:); 

顯然,這確實不工作。我認爲必須有一個優雅的解決方案?

回答

1

如果inputData是一個單元陣列,那麼你可以這樣做:

[x y currentSampleTime velocityX velocityY] = deal(inputData{i,:}); 

但是,因爲你是用索引變量i行,我可以認爲這是一個for循環內?

如果是這樣,我只會在循環之前執行以下操作?

x = inputData(:,1); 
y = inputData(:,2); 
currentSampleTime = inputData(:,3); 
velocityX = inputData(:,4); 
velocityY = inputData(:,5); 

就用x(i)y(i)等你的循環內。

...或取決於生成inputData的方式嘗試在讀入或創建inputData時創建必要的數組。

此外,在個人記錄中,我不喜歡使用i作爲m代碼中的變量,因爲如果未正確初始化,它很容易與虛數混淆。

0

試試這個:

wh = size(inputData); 
temp = mat2cell(inputData,wh(1),ones(1,wh(2))); 
[x,y,currentSampleTime,velocityX,velocityY] = deal(temp{:});