Create a new variable when conditions are met
Show older comments
Hello All,
A question from a novice here that has been working on what no doubt many of you will consider a very simple problem - but it still eludes me!
In a nutshell, I am trying to achieve the following:
- write a function that will create a new variable when certain conditions are met when looking at an existing variable
Example
Temp is a matrix (177,2) with varying data. When the function [datagrab] is called, if a number appears in the second column that matches the conditions eg '1' (this is not a simple logical test as other integer numbers could be 2,3,4,5 etc.), then that whole row should be copied to a new variable 'G'. If the datagrab function is called again say using datagrab('2','M') - then all rows of data from temp that have the value 2 in the second column would be written to a new variable called 'M', and so on. I hope that makes sense!
I have been trying to just get it to work with '1' and 'G' for now, and I have this:
function [outindex] = datagrab(1,G)
n = 1;
outarray = [];
for G = length(temp)
if (temp(:,2) == '1'); %eg whatever value set as '1' for now
outarray(n)=G;
n = n+1;
else end
end
end
but its just not working for me. So, if anyone could offer a solution for the whole problem, or just a fix to my code above then I will be most grateful.
Heres hoping!
10B.
4 Comments
Steven Lord
on 9 Sep 2015
What you're trying to do sounds a lot like something that we strongly DISCOURAGE people from doing. See question 1 in the Programming section of the FAQ for more information.
Image Analyst
on 10 Sep 2015
Well I thought of that at first too Steven, but then I thought of some cases where it might be used. For example the max() function can return the max, and optionally the index where the max occurs. So I suppose if you did not ask for the index, it wouldn't need to calculate it. Does it calculate it anyway even though it knows (at some point in time) that it should not return it?
Walter Roberson
on 10 Sep 2015
Some routines calculate the extra variables anyhow and some do not. Mostly if the calculation of the variable is "expensive" then the calculation is skipped.
But that does not have to do with creation of variable names dynamically. Each potential output has a name, either named specifically on the left side of the "=" or as varargout{K} for some K. The variables are in the function workspace. When the function returns and there is an output argument to receive the value, everything except the name is copied over (and the reference count is fixed up if appropriate); if there is no slot to receive the value then it is discarded. Nothing dynamic about that.
10B
on 10 Sep 2015
Accepted Answer
More Answers (1)
Walter Roberson
on 10 Sep 2015
function datagrab(condition, varname)
temp = .... some way of getting in the matrix
find some matches and put their indexes into idx
selected = temp(idx,:);
assignin('caller', varname, selected)
end
1 Comment
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!