フィルターのクリア

save data from function to ws

7 ビュー (過去 30 日間)
Bou
Bou 2014 年 2 月 20 日
コメント済み: Bou 2014 年 2 月 20 日
Hello,
I want to save the data of a 8x1 matrix to the workspace from a function. However, the variables in the matrix change every time the function is called, while the name of the Matrix stays the same.
I tried assignin. This works well but now I want to create a matrix consisting of a row of 8x1 variables. I actually want the following;
assignin('base','Pars2(end+1,:)',Pars)
But this doesnt work.
Can anybody help me? Thanks in advance!
Greetings Boudewijn

採用された回答

Iain
Iain 2014 年 2 月 20 日
You have three options.
1. Use evalin or assignin correctly to put the variable into the base workspace.
2. Change your function to a script, and just directly work in the calling workspace.
3. Return everything you want your function to return, as an argument.
e.g.
function [cosine_result, sine_result, tangent_result] = trigfcns(angles)
cosine_result = cos(angles);
sine_result = sin(angles);
tangent_result = cosine_result ./ sine_result;
Then call your function like:
[cosses, sines, tans] = trigfcns([45 31 91 64]*pi/180);
From a programming quality/best practice point of view, the advice would be to just use the 3rd option. - Using varargout & nargout as ways of not returning optional output.
  1 件のコメント
Bou
Bou 2014 年 2 月 20 日
thanks for the help!
I finally created a global variable and did the following
global Pars2;
Pars2(end+1,1:length(Pars)) = Pars;
that did the job:)

サインインしてコメントする。

その他の回答 (1 件)

Dishant Arora
Dishant Arora 2014 年 2 月 20 日
As the documentation suggests, 2nd parameter has to be only the variable name, it cannot have any indices. Better assign that value to a new variable in workspace and later on add it to existing matrix.
assign('base' , 'newVar', val);
existingMat(end+1,:)=newVar;

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by