フィルターのクリア

How do I insert many variables(upto 100) into one function?

4 ビュー (過去 30 日間)
Hoon
Hoon 2014 年 2 月 25 日
コメント済み: Walter Roberson 2014 年 2 月 25 日
I have variables with value
B1=1 B2=2 B3=4 B4 ... B100= some number
(it goes on to 100 variables)
then I want to insert all of these variables into one function
save('filename.mat', 'B1', 'B2', 'B3'.... 'B100')
However, it will be very counterproductive to write all the 100 variables...
how do i shorten this function?
  1 件のコメント
John D'Errico
John D'Errico 2014 年 2 月 25 日
Perhaps this is a good time for you to learn NOT to name all of your variables like that, and instead learn how to use a vector. Vectors are new things in MATLAB, I think introduced in version 1 of the software, so really rather new.

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

採用された回答

Rick Rosson
Rick Rosson 2014 年 2 月 25 日
B = zeros(100,1);
B(1) = 1;
B(2) = 2;
B(3) = 4;
B(4) = ...
B(5) = ...
...
...
B(100) = ...
save('filename.mat', 'B');
  4 件のコメント
Hoon
Hoon 2014 年 2 月 25 日
hmm... I think I haven't specified my question in detail. in my real program, each variable will hold vectors of different size.
Your answer seems like concaternating all the variables, so maybe I should save concaternated values and load each column.
conclusively I should concern more about separating columns of saved value. Thank you for your approach.
Walter Roberson
Walter Roberson 2014 年 2 月 25 日
If the vectors are different sizes then you could use a cell array.

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

その他の回答 (2 件)

Star Strider
Star Strider 2014 年 2 月 25 日
This seems a bit inelegant but it works:
B1=1; B2=2; B3=4;
for k1 = 1:3
B(k1) = eval(sprintf('B%d', k1));
end
You would then save vector B in your save command.
  2 件のコメント
Hoon
Hoon 2014 年 2 月 25 日
sorry, but I want to load each variables once i saved them...
but i have to think otherwise
Star Strider
Star Strider 2014 年 2 月 25 日
編集済み: Star Strider 2014 年 2 月 25 日
You can always refer to them by their subscripts, since those correspond to the names you’ve given the original variables.
With respect to vector B:
B(1) = B1;
B(2) = B2;
.
.
.
B(100) = B100;
Then, for example, refer to B(1) in your code rather than B1, etc.
Unless there is some problem with this (creating a vector B out of the original set of B variables) that you are not telling us about, this is not only easier to work with but much more efficient.
If the B variables are instead vectors of different lengths, and you are having problems creating a matrix from them, MATLAB can do that very efficiently with cell arrays.
We need to know what you want to do in much more detail, and specifically why creating the B vector does not work for you.

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


Walter Roberson
Walter Roberson 2014 年 2 月 25 日
save('filename.mat', 'B\d+')
This syntax is not well documented in the save() document. It is just barely hinted at: notice that there is an entry for "regular expression" in the Tips section of the page.

カテゴリ

Help Center および File ExchangeWhos についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by