Hi,
I have a cell which contains twenty different names( Names<1x20 cell>),there is also twenty variables in my workspace like : VarName1 VarName2... I need to assign those names to these variables,the point is that I can not just overwrite them because I have values in variables and do not want to loss them.
any help would really appreciated.

 採用された回答

Image Analyst
Image Analyst 2014 年 5 月 10 日
編集済み: Image Analyst 2014 年 5 月 10 日

1 投票

A cell cannot contain 20 different names unless the cell contains another cell, or a character array (which means all the names are the same length).
Your "Names" variable can be a 1x20 cell array - that is, an array of cells . Each cell (of the 20 cells in the array) may contain one string, or one of anything else, such as another cell, a double matrix, a structure, a table, etc. Please read the FAQ for an explanation of what cells are and how they work. http://matlab.wikia.com/wiki/FAQ#What_is_a_cell_array.3F
Not sure why you're asking about cells at all. If you have 20 named variables like VarName1 VarName2, and so on, you can rename them and retain their data/values like this:
newVar1 = VarName1; % Copy to new variable with new and different name.
clear('VarName1'); % Delete old variable with old name.
newVar2 = VarName2;
clear('VarName2');
newVar3 = VarName3;
clear('VarName3');
and so on down to 20.
If you want those 20 separately named variables in a new cell array , with one variable in each cell, you can do this:
ca{1} = VarName1; % Copy to new variable with new and different name.
clear('VarName1'); % Delete old variable with old name.
ca{2} = VarName2;
clear('VarName2');
ca{3} = VarName3;
clear('VarName3');

4 件のコメント

Image Analyst
Image Analyst 2014 年 5 月 11 日
OK I opened your mat file and it's simply a 1 row by 55 column cell array (a row vector of cells). In each cell is simply a string. There may be variables in the program that have the same name as the strings in the 55 cells, but what you have in the cells are simply strings . So you can just reassign them to whatever you want. In your example of the third cell, the string is already "IST_WANDLER_MOM" so there's nothing to change. But if you did , say you wanted to change it to "IST_WANDLER_DAD" you'd simply do
ind2{3} = 'IST_WANDLER_DAD'
Note that you are not renaming any variables at all, you're simply changing a string inside a cell (the 3rd cell of the 55-cell cell array).
Navid
Navid 2014 年 5 月 11 日
thanks for your answer,I think I did not explain enough,as I mentioned before,I have also 55 different variable which contains values, in my workspace like: VarName1,Varname2,...VarName55. I need to assign the names which are in vector to these VarName1-55. for example the name of VarName3 should be ind{3},but I can not just write VarName3=ind{3} because it overwrites and I lose the values which are in VarName3.
Image Analyst
Image Analyst 2014 年 5 月 11 日
This is very strongly recommended against. You should just avoid the whole situation in the first place. If you insist on ignoring recommendations, then you can use the third example in the FAQ (as Azzi referred you to yesterday):
for i=1:10
eval(sprintf('A%d = [1:i]', i));
end
This is what it would look like:
IST_WANDLER_MOM = 42; % Initialize with something
eval(sprintf('%s_newName = %s', ind2{3}, ind2{3}));
Assuming that all those variables already exist somehow, you'd do the above in a loop
for k = 1 : length(ind2)
eval(sprintf('%s_newName = %s', ind2{k}, ind2{k}));
end
At some point you'll eventually realize why it's recommended not to do that.
Navid
Navid 2014 年 5 月 12 日
Thank you so much for your help.

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

その他の回答 (3 件)

Star Strider
Star Strider 2014 年 5 月 10 日

1 投票

The solution to How can I use a for loop to reassign matrix names? may work for you.
Navid
Navid 2014 年 5 月 10 日

0 投票

thanks for your respond,but it does not work,as I mentioned before,I have an array which contains of different names,I will attach my array here,maybe it helps.

4 件のコメント

Star Strider
Star Strider 2014 年 5 月 10 日
What ‘doesn’t work’?
  • What method are you using?
  • What does it do that it shouldn’t
  • What doesn’t it do that it should?
  • Are you getting any errors?
Note that some of them, for example ‘ 'IST_HGH_RES_TRIP...' ’ are not valid variable names in MATLAB and will throw an error. It seems as though those names are truncated. You will at least have to get — and use — the entire variable name before any method will work.
Navid
Navid 2014 年 5 月 10 日
I think the variable names are correct,I can get the entire name,for example the third one is: >> ind2{1,3}
ans =
IST_WANDLER_MOM
now I need to change the name of VarName3 to "IST_WANDLER_MOM"
Image Analyst
Image Analyst 2014 年 5 月 11 日
Star, you need to widen the columns to get rid of the ... and see the whole string.
Star Strider
Star Strider 2014 年 5 月 12 日
They were that way in the cell. I used char to see the entire string.

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

カテゴリ

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

質問済み:

2014 年 5 月 10 日

コメント済み:

2014 年 5 月 12 日

Community Treasure Hunt

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

Start Hunting!

Translated by