Set and read matrix from 'setenv'

Dear,
I have some medium input matrices that frequently change as program input. I want to move them out to external file by using 'setenv' or similar. Is that possible ? or any alternative way ?
Many thanks,
-Dan

2 件のコメント

Guillaume
Guillaume 2018 年 12 月 18 日
"I want to move them out to external file"
What does that mean?
"using 'setenv' or similar"
setenv is used to modify environment variables. This has nothing to do with files.
Can you describe what you want to do (and why) in a lot more details.
Dan NG
Dan NG 2018 年 12 月 18 日
My code has matrices of 3x10 (s) that need to change everytime as program input.
I have to modify code everytime for each run.
Is anyway that i can set it to variables so i can read them back ?
or I have to create .dat or .txt file and import them ?
I'm try to separate code and data.
thanks,

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

回答 (1 件)

Guillaume
Guillaume 2018 年 12 月 18 日

0 投票

It sounds like you have a script where all the inputs are hardcoded. First, convert the script into a function with the appropriate inputs and outputs.
E.g. If you have a script like:
%inputs
something = [1 2;3 4];
somethingelse = [10 20; 30 40];
%processing code
someresult = something . ^ 2;
someotherresult = something + sqrt(somethingelse);
then it becomes:
function [someresult, someotherresult] = dosomething(something, somethingelse)
someresult = something . ^ 2;
someotherresult = something + sqrt(somethingelse);
end
After that it's up to you how you get the various inputs to the function. You could write a script will all the inputs and loop over them:
%all the matrices to process
manysomething = {[1 2;3 4], [5 6;7 8], [9 10 11; 12 13 14]};
manysomethingelse = {[10 20; 30 40], [50 60; 70 80], [90 100 110; 120 130 140]};
%preallocate results:
manyresults = cell(size(manysomething));
manyotherresults = cell(size(manysomething));
%loop over inputs
for idx = 1:numel(manysomething)
[manyresults{idx}, manyotherresults{idx}] = dosomething(manysomething{idx}, manysomethingelse{idx});
end
Or you can get these inputs from text file, from spreadsheets, or from a GUI.

1 件のコメント

Dan NG
Dan NG 2018 年 12 月 18 日
Thank you Guillame,
I'd like to read all input from text file. How can I do that ?

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

カテゴリ

ヘルプ センター および File ExchangeMATLAB についてさらに検索

製品

リリース

R2015b

タグ

質問済み:

2018 年 12 月 18 日

コメント済み:

2018 年 12 月 18 日

Community Treasure Hunt

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

Start Hunting!

Translated by