MATLAB Answers

Calling a function using a string generated earlier in code.

2 views (last 30 days)
Ben Clark
Ben Clark on 26 Jul 2012
What I want to do is call a function using a string that was generated earlier in the code. The initial function takes user input to create the string for the internal function. Here is what I have so far:
function [Cmdty2Pull]=refresh_AllComdtyCloses(Cmdty2Pull)
LastPullTable = [Cmdty2Pull '_History'];
I want to then do the following
irow = length(LastPullTable(:,1)
where irow is set to length of cell array CL_History if the user inputs CL.
Is there an easy way to do this that I just dont know about?


Jonathan on 26 Jul 2012
How many different calls will you be making? How many "_History" cells are there?
Ben Clark
Ben Clark on 26 Jul 2012
I currently have 3 cell arrays using this naming convention but moving forward there may be more added.
It will most likely be called in a few other places in the code.
Hope this answers your questions.
Jonathan on 26 Jul 2012
Let me know if the solution below works.
Have a good one, Ben!

Sign in to comment.

Accepted Answer

Jonathan on 26 Jul 2012
Here is a solution that will work depending on how many different cells you're talking about.
Define the cell (in any function) like this:
setappdata(0,'CL_History',{CELLDATA HERE});
and call it like this:
irow = length(getappdata(0,LastPullTable));
The problem is (how I see it) that you have to be using some sort function to reference a string or else "length" will just give you the length of the string.
This solution will work, but it may not be efficient or reasonable depending on what kind of data you're dealing with and the amount present.


Show 1 older comment
Jonathan on 26 Jul 2012
Whatever data is in CL_History, you said it was a cell so that's what I went with. Let me show you an example.
setappdata(0,'My_Data',[1 1 1 2 2 2 3 3 3]);
setappdata(0,'My_String','Test String');
setappdata(0,'a_Cell',{'a' 'cell' 'of' 'strings'});
Copy and paste the lines above into your command window to see how get/set appdata works. It's basically storing information in the ML root.
setappdata(where, name, data)
if you originally defined CL_History like:
CL_History = {1 0 1 1};
just modify it to
setappdata(0,'CL_History',{1 0 1 1});
Jonathan on 26 Jul 2012
Happy to help! Maybe someone else has a better method of solving, but I've fallen in love with the setappdata command over the last few days!
You could also create an m-file for each cell and use feval:
function [output] = CL_History()
CL_History = {cell};
output = length(CL_History);
and in your original function:
irow = feval(LastPullTable);

Sign in to comment.

More Answers (0)

Sign in to answer this question.