How do I pass the output of a command as argument to the other command?
Afficher commentaires plus anciens
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
yashwant kolluru
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:
- the dimensions of numeric arrays: fully vectorized code is often the fastest MATLAB data processing!
- cell arrays
- structures, where you can include fields for each kind of data (e.g. Process type, Flow data, Temperature data, Notes, Units, etc). Structures can even be non-scalar, which is extremely useful! And yes, you can even define structure fieldnames dynamically.
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":
Réponse acceptée
Plus de réponses (1)
Image Analyst
le 27 Oct 2015
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!