How do I pass the output of a command as argument to the other command?

I have created char array let say
a= sprintf('%s%d','elem_append_mod',6);
Now I already have an cell array of size 1 X 12 with the name "disp(a)".
When I execute the command
size(sprintf('%s%d','elem_append_mod',6),2)
I get the output as 16 (length of the string 'elem_append_mod6'). but what I require is the size of the cell array with the name 'elem_append_mod6' i.e. 12 !

2 commentaires

Thanks Guys! for yor help!
Stephen23
Stephen23 le 27 Oct 2015
Modifié(e) : Stephen23 le 27 Oct 2015
Do NOT create or access variable names dynamically
Ugh... this is a really bad idea!
Avoid creating dynamically named variables in MATLAB. This is poor practice as has been explained many times on this forum, and is not recommended by MATLAB themselves:
When you are a beginner it seems like a cunning and fast way to store information, but actually it is really bad practice to name your variables dynamically. MATLAB is also not intended for this kind of variable naming: if you continue to include data in the variable names then you will find yourself fighting many more of these battles against MATLAB.
However when you use more appropriate storage for your data (and meta-data) then you will suddenly find lots of MATLAB functions that do many useful operations for you, quickly and easily.
In your case a much more robust solution would be to use:
There are many functions that support working on structures and cell arrays and can access their data easily, and many functions operate on complete numeric arrays all at once without any loops (i.e. vectorized code, which is something you need to learn about).
Placing your data in a structure or cell array also makes it much easier to pass to functions: can you imagine the fight you would have trying to pass hundreds of dynamically named variables to a function?
If you have a newer version of matlab you can also use a table , which stores the data together in one array but also allows key-name access to the columns. This might be a good alternative for your data.
In case you are interested, here are some pages explaining why dynamically assigning variable names is a really bad idea in MATLAB:
Here is a discussion of why it is a bad idea to include meta-data (such as an index) in a variable name:
And for some other languages advising "DO NOT create dynamic variable names":

Connectez-vous pour commenter.

 Réponse acceptée

Thorsten
Thorsten le 27 Oct 2015
Modifié(e) : Thorsten le 27 Oct 2015
You have to use eval:
eval(['size(' sprintf('%s%d','elem_append_mod',6) ', 2)' ])
BTW: If you use a fixed constant like 6, instead of
a= sprintf('%s%d','elem_append_mod',6);
you can simply write
a = 'elem_append_mod6';
And it seems that you dynamically create variable names, which is not a good idea.

4 commentaires

yes! it worked! thanks so much!
And using eval is a really bad idea. See the hundreds of concerning threads in this forum.
I'm lost. I get an error:
>> eval(['size(' sprintf('%s%d','elem_append_mod',6) ', 2)' ])
Error using eval
Undefined function or variable 'elem_append_mod6'.
So why was this accepted, and where is the 12? What line of code generates a 12? And why 12 anyway? What's 12?
Thorsten
Thorsten le 27 Oct 2015
Modifié(e) : Thorsten le 27 Oct 2015
The question was not 100% clear. I interpreted it such that yashwant has created a variable named 'elem_append_mod6' and now wanted to get the size of this variable (see the last line of the question).

Connectez-vous pour commenter.

Plus de réponses (1)

Cell arrays cannot be named disp(a) because variable names cannot have parentheses in the names. So I will assume your cell array is called "ca" rather than "disp(a)". You can, however, have a cell array with the name "elem_append_mod6". The cell array can have 12 cells in it if you want.
I suspect the difficulty lies in you not completely understanding cells and cell arrays. I think the FAQ will help tremendously, so please read it: http://matlab.wikia.com/wiki/FAQ#What_is_a_cell_array.3F
Then run this illustrative demo which kind of does what you say, but not what you want since I don't know what that is and don't know where the 12 comes from:
% Create a simple string variable.
a = sprintf('%s%d','elem_append_mod',6)
sa = size(a)
la = length(a)
whos a
% Create a cell array, called "ca", with a size of 1-by-12
ca = cell(1, 12); % Make 12 empty cells.
% Put a into cell #1
ca(1) = {a}
size(ca)
whos ca
% Now make another cell array with the name "elem_append_mod6"
elem_append_mod6 = cell(1, 12); % Make 12 empty cells.
% and put "a" into that cell array's first cell:
elem_append_mod6(1) = {a}
size(elem_append_mod6)
whos elem_append_mod6
In the command window:
a =
elem_append_mod6
sa =
1 16
la =
16
Name Size Bytes Class Attributes
a 1x16 32 char
ca =
'elem_append_mod6' [] [] [] [] [] [] [] [] [] [] []
ans =
1 12
Name Size Bytes Class Attributes
ca 1x12 232 cell
elem_append_mod6 =
'elem_append_mod6'
ans =
1 1
Name Size Bytes Class Attributes
elem_append_mod6 1x1 144 cell

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by