Feeding strings into function xcov without quotation marks
Show older comments
Dear all,
I'm trying to compare various signals (vectors) using the function xcov. The names of these vectors all have the form "condition_meanrate", with the prefix "condition" being variable (so e.g. I have a files called 'cedo_meanrate', sodo_meanrate', 'sosp_meanrate' etc. The variable prefixes are stored in a separate vector). I created for-loops and am using
arg_1=sprintf('%s',x,'_meanrate')
arg_2=sprintf('%s',x,'_meanrate')
(with x being a char array containing the respective prefix) to feed the respective signal-vector into the xcov function:
[xcov_vec,lags]=xcov(arg_1,arg_2,'coeff').
Now the problem is that the names are fed in the way as if I'd type them using quotation marks, so like xcov('cedo_meanrate','sosp_meanrate','coeff'). Then xcov seems to compare the actual strings instead of the signal values. If I manually type xcov(cedo_meanrate,sosp_meanrate,'coeff') it works. I found a similar problem here: https://answers.yahoo.com/question/index?qid=20120416085207AAsXhqN But looking into the xcov script it says:
function [xycov,lags] = xcov(x,y,option1,option2)
I don't really know what to do now. Is there a way to feed the names into the function so that they appear as if the didn't had quotation marks?
Thanks very much! Best, Felix
Accepted Answer
More Answers (1)
Stephen23
on 1 Apr 2015
0 votes
"Is there a way to feed the names into the function so that they appear as if the didn't had quotation marks?"
Probably
Is it a good idea?
No
Creating variables like this is poor programming style, and will only make your own life difficult. Just like many things in life, just because you can do something is not synonymous with it being a good idea to do it. This is poor practice as has been explained many times on this forum, and is not recommended by MATLAB themselves:
In case you are interested, here are some pages explaining why dynamically assigning variable names is a really bad idea in MATLAB:
Categories
Find more on Matrices and Arrays 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!