Create variable named from a cell

How do I generate a variable of empty matrix, which is named after the values in one of the cell of imported text in another variable.
E.g I import data file with
A {'DT140906' 'x'};
Now I want to create a varible that will automatically take the name 'DT140906' or what ever the name is in A.

 採用された回答

the cyclist
the cyclist 2014 年 9 月 22 日

0 投票

A = {'DT140906' 'x'};
eval([A{1},' = {}'])

3 件のコメント

Mandeep
Mandeep 2014 年 9 月 22 日
Thank you! This works
Now the only thing is that I like to make this into an empty matrix and How do I do that?
Image Analyst
Image Analyst 2014 年 9 月 23 日
Why is it not empty??? Essentially you're doing
DT140906 = {};
and making a cell (one cell, not a cell array ) with null (empty) contents. Why is your DT140906 variable not empty? What value does it have??? Maybe you want a double scalar instead?? In that case, do
eval([A{1},' = []']); % Brackets instead of braces.
Again, it will be empty.
Mandeep
Mandeep 2014 年 9 月 23 日
Yup this works

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

その他の回答 (1 件)

Image Analyst
Image Analyst 2014 年 9 月 22 日

0 投票

And how are you going to be able to refer to that variable later in your code when you don't know the name of it until run-time ? You can use dynamic field names in structures but I don't even like that approach.

2 件のコメント

the cyclist
the cyclist 2014 年 9 月 22 日
I was going to wait until the expected "that doesn't work" or rarer "thanks!" before I launched into the fact that what you are doing is likely a bad programming practice, but I agree with Image Analyst here.
If you have any control of how these variables are named upstream from your program, you might be able to do better in the MATLAB section of what you are doing. We'd need to know more detail to help you figure out a strategy.
Mandeep
Mandeep 2014 年 9 月 22 日
I will put the code in the window and see if that makes a better sense.

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

カテゴリ

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

質問済み:

2014 年 9 月 22 日

コメント済み:

2014 年 9 月 23 日

Community Treasure Hunt

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

Start Hunting!

Translated by