Passing string as function variable for renaming table column

조회 수: 11 (최근 30일)
jcledfo2
jcledfo2 2017년 12월 13일
댓글: jcledfo2 2017년 12월 14일
I am trying to pass a string variable to a custom function to rename a table column title.
My code is below:
temperature = 'OPC.Temperature.BottomTemperature';
temp_column_name = 'Temperature';
%extract bottom temperature data
data_temp = dataextract(data_temp,temperature,temp_column_name);
Below is the function:
function [ data ] = dataextract( data_array,variable,column_name )
%remove all rows not containing variable in column 2
data_string = variable;
data_comp = strcmp(data_array(:,2),data_string);
data_comp = ~data_comp;
data_array(data_comp,:) = [];
%remove extra information from columns 3 & 4 from data array
data_array(:,(2:4)) = [];
%convert data_array cell array to table
data_array = array2table(data_array,'VariableNames',{'TimeStamp', column_name});
%convert timestamp string to datetime
data_array.TimeStamp = datetime(data_array.TimeStamp,'InputFormat','yyyy-MM-dd HH:mm:ss.SSS');
%convert beam current string to double precision value
data_array.column_name = str2double(data_array.column_name);
data = data_array;
end
I tried to initially just pass 'Temperature' in the function but that would not work and now a variable won't pass either. Below is my error:
Error using dataextract (line 18)
Unrecognized variable name 'column_name'.
Error in parser_test (line 38)
data_temp = dataextract(data_temp,temperature,temp_column_name);

채택된 답변

Walter Roberson
Walter Roberson 2017년 12월 13일
Change
data_array.column_name = str2double(data_array.column_name);
to
data_array.column_name = str2double(data_array.(column_name));
  댓글 수: 1
jcledfo2
jcledfo2 2017년 12월 14일
Thanks!!
I had to also add () to the beginning or it added an extra column
data_array.(column_name) = str2double(data_array.(column_name));

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by