How to get values assigned to a string?
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi all,
My data are stored as below in the workspace:
white_left 400x129
white_right 400x129
black_left 400x90
black_right 400x90
Now I have to call each and do some operations in each column:
type = {'white','black'};
orientation = {'left','right'};
addOne = 0;
for a = 1:length (type)
for b = 1:length (orientation)
for j = 1:size([neuron_type{a} '_' orientation{b}]), 2) % Suppose I want to repeat some operation for the number of columns in each dataset
addOne = addOne + 1; % DO SOME OPERATION
end
end
end
But already in
size([neuron_type{a} '_' orientation{b}]), 2)
it does not find the size of data which should be 129 but it finds the size of string for example 10 for "white_left".
How can I make Matlab understand that I want the data.
Thanks a lot :)
1 Kommentar
Stephen23
am 5 Aug. 2015
@Max: read the comments that you are getting here: "do not use eval", and then have a read of the links that I gave you. You will learn something today if you try and understand why using eval is not a good way to solve this problem. Use cell arrays instead.
Antworten (2)
Image Analyst
am 5 Aug. 2015
See the FAQ to understand how cell arrays work: http://matlab.wikia.com/wiki/FAQ#What_is_a_cell_array.3F
orientation(b) is a cell but orientation{b} is the contents of the cell, which is a string.
neuron_type is not defined so we don't know what is in the cells of that cell array.
Anyway, you would not use a nested for loop for this kind of thing. You'd write a function (for example AnalyzeData) and just pass in the arrays
results = AnalyzeData(white_left)
results = AnalyzeData(white_right)
results = AnalyzeData(black_left)
results = AnalyzeData(black_right)
In AnalyzeData(), you'd extract and process the desired columns of whatever matrix got passed in.
3 Kommentare
Stephen23
am 5 Aug. 2015
Ugh... 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 these data easily, and they can also be used in 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":
1 Kommentar
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!