2012-12-27 42 views
-2

好的問題是,我想要接收數學函數。在程序運行之前我不知道有多少人。如何創建一個接受字符串的向量?

當它運行時,我要求一個n函數的數量,我會收到它並開始保存它們從輸入。

到目前爲止,我有這個

function test() 
n = input('number of equations?'); 
v = [1:n] 
%in an ideal world, this^here would allow me to put a string in each position but 
% they are not the same type and I understand that.. but how can I build a vector for saving my    functions 
%I want a vector where I can put strings in each position that is what I need 
for i=1:n 
x = input('what is the function?','s'); 
v(i)=x 
end 
v 
%this would be my vector already changed with a function in each position. 
end 

回答

2

當你想存儲不同長度的字符串,請使用電池陣列:

v = cell(1,n); 
for i=1:n 
    v{i} = input('what is the function?','s'); #% note the curly braces 
end 

要使用這些作爲功能,使用str2func

for i=1:n 
    fh{i} = str2func(v{i}); 
end 

fh現在是一個單元格數組,包含fu的句柄由用戶輸入字符串定義的節點。

+0

非常感謝你! – user1907948

相關問題