How to use correctly a string from a variable into a for loop??

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

Great Walter than you for the help.
I knew that eval shouldn´t be used, but not that we shouldn´t create dynamic variables.
Then I'll use array indexing or struct (I like the struc more because I can put headers) but then, the question still is the same, how can I access that data??
I mean, if I have everything in columns, I still have that column named "distancelap" which is a string inside Ch_sLap. How can I go straight to the 'distancelap' column inside the array using a for loop?? (I will then put it in a structure).
Thank you!
You do not have an array, you have a table. And those table variables do not have newlines in their names.
Channels_struct = table2struct(CSV_Import(:,Channels));
This does not do checking to be sure everything in Channels is in CSV_Import; you would get an error if there were a problem.
Or if you prefer for some reason:
Channels_struct = struct();
known_channels = find( ismember(Channels, CSV_Import.Properties.Variablenames) );
for K = known_channels
Channels_struct.(Channels{K}) = CSV_Import.(Channels{K});
end
This filters down to only the Channels that are in the table; it also illustrates copying one at a time, which might be of interest to you for other reasons.
I've been checking what you proposed Walter and it still gives me the same problem. I may not explained myself correctly:
In the Channels table one of the column is named "distancelap".
That name ("distancelap") is inside the Ch_sLap variable, I mean:
Ch_sLap = 'distancelap'
What I want is to find the 'distancelap' column in the table from a command. If I use this:
Channels_struct = table2struct(CSV_Import(:,Channels));
it tries to find Ch_sLap in the table, but what I want to find is 'distancelap' (The string placed inside Ch_sLap).
I hope not to be a pain...
Thank you!!

Answers (1)

By writing below mentioned code you are creating a string variable with value as 'Ch_sLap' and storing in cell array Channels. Hence, it takes column name as 'Ch_sLap'.
Channels={'Ch_sLap'}
I created a table mentioned in the first example on this page and tried your code. I changed "Channels" array into the following way (variable 'Ch_sLap' has value 'Age') :
Channels={Ch_sLap}
for i=1:length(Channels)
myTable.(Channels{i})
end
Above code was able to print values present in 'Age' column.

This question is closed.

Asked:

on 23 May 2020

Closed:

on 20 Aug 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!