Hi all,
Firstly thank you for your time!
I'm working in a matlab script for data analysis and plotting from a csv file. I'm trying to use a variable name into a for loop to get the data on a specific table column but I can't get it right...
A simplified version of my code is:
CSV_Import=readtable('Data.csv');
Ch_sLap ='distancelap';
Channels={'Ch_sLap'}
sLap = CSV_Import.(Ch_sLap);
for i=1:length(Channels)
if exist(char(Channels(i)))
char(Channels(i)) = CSV_Import.((fprintf('%s\n', Channels{i})));
end
end
What I want to do is:
- Import a csv file with some data arranged in columns with headers into a table
- Find the column which is named "distancelap" on that table
- That column header (distancelap) is coming from an array of strings that I've created previously
- Then, if I find the column using this: sLap = CSV_Import.(Ch_sLap); is working fine
- But instead of that, if I use the for loop (In case I had a long Channels array) I can't make it to work because it takes "Ch_sLap" instead of its value which is "distancelap"
I can do it without the loop and just put one by one all the variables of the Channels array but I don't like it at all...
What do you think???
Thank you!!
5 Comments
Direct link to this comment
https://de.mathworks.com/matlabcentral/answers/531348-how-to-use-correctly-a-string-from-a-variable-into-a-for-loop#comment_859248
Direct link to this comment
https://de.mathworks.com/matlabcentral/answers/531348-how-to-use-correctly-a-string-from-a-variable-into-a-for-loop#comment_859248
Direct link to this comment
https://de.mathworks.com/matlabcentral/answers/531348-how-to-use-correctly-a-string-from-a-variable-into-a-for-loop#comment_859403
Direct link to this comment
https://de.mathworks.com/matlabcentral/answers/531348-how-to-use-correctly-a-string-from-a-variable-into-a-for-loop#comment_859403
Direct link to this comment
https://de.mathworks.com/matlabcentral/answers/531348-how-to-use-correctly-a-string-from-a-variable-into-a-for-loop#comment_859468
Direct link to this comment
https://de.mathworks.com/matlabcentral/answers/531348-how-to-use-correctly-a-string-from-a-variable-into-a-for-loop#comment_859468
Direct link to this comment
https://de.mathworks.com/matlabcentral/answers/531348-how-to-use-correctly-a-string-from-a-variable-into-a-for-loop#comment_860338
Direct link to this comment
https://de.mathworks.com/matlabcentral/answers/531348-how-to-use-correctly-a-string-from-a-variable-into-a-for-loop#comment_860338
Direct link to this comment
https://de.mathworks.com/matlabcentral/answers/531348-how-to-use-correctly-a-string-from-a-variable-into-a-for-loop#comment_860638
Direct link to this comment
https://de.mathworks.com/matlabcentral/answers/531348-how-to-use-correctly-a-string-from-a-variable-into-a-for-loop#comment_860638
Sign in to comment.