I have an array of numeric data, and I have another with the string headers of that data. these two arrays match in terms of what header goes with which column. I want to write an if statement that says for each column in the headers array, if the string value in column 1 for example equals 'header1', then take column 1 from the data array and multiply all values by 10 (or apply some function to the data in each cell). if it equals a different string do something else, and then move on to the next string in the header array and repeat the process.
Here is what I have so far. names is the array with 1 column and some string headers and Data is the new data file, while dataToRead is the original data array. there must be a betetr way to do this, I might have anwyhere from 1 to 20 formulas/headers so making a million if else statements seems wrong. Somewhere down the line I would like to have a repository of formulas that can be applied for each string case so that I can have it cycle through instead of having 20 if statements. is that possible?
This code seems to work just fine, but it cant be the best way to go about this.
for n=1:length(names)
if names{:,n}=='header1'
Data(:,n)=3*DataToRead(:,n+1); %I have a time stamp in column 1 i dont want to act upon
else
if names{:,n}=='header2'
Data(:,n)=4*DataToRead(:,n+1)
else
if names{:,n}=='header4'
Data(:,n)=0*DataToRead(:,n+1)
end
end
end
end
Aucun commentaire:
Enregistrer un commentaire