Table: Add collumn with conditional

조회 수: 1 (최근 30일)
Nycholas Maia
Nycholas Maia 2019년 1월 7일
댓글: Nycholas Maia 2019년 1월 7일
How to modify this function to create a better code think in how to get a dynamic output table.
In this simple example below, I have only 3 possible input arguments, but in real-life I have 20 possible input arguments.
function table_out = dynamic_table(varargin)
% Set the valid function input arguments:
input_options = {'array_a', 'array_b', 'array_c'};
% Set the index values of each function input argument:
ARRAY_A_IDX = 1;
ARRAY_B_IDX = 2;
ARRAY_C_IDX = 3;
% Preallocate a valid output table arguments:
output_options = zeros(length(input_options), 1);
% Check each function input argument:
for i = 1:length(varargin{:})
% Check if the input(i) is a valid input option:
[is_valid, opt_idx] = ismember(char(varargin{:}(i)), input_options);
if(is_valid)
% If is a valid option, set as true:
output_options(opt_idx) = true;
else
% This input(i) is not a valid function input argument:
warning('Unable to find the entered field: %s', char(varargin{:}(i)));
end
end
% Set a dummy array content:
array_a = [1; 2; 3];
array_b = [4; 5; 6];
array_c = [7; 8; 9];
%% Construct a dynamic conditional output table:
if((output_options(ARRAY_A_IDX)) & (!output_options(ARRAY_B_IDX)) & (!output_options(ARRAY_C_IDX)))
% Only one output collumn:
table_out = table(array_a);
elseif ((output_options(ARRAY_A_IDX)) & (output_options(ARRAY_B_IDX)) & (!output_options(ARRAY_C_IDX)))
% Two output collumns:
table_out = table(array_a, array_b);
elseif ((output_options(ARRAY_A_IDX)) & (output_options(ARRAY_B_IDX)) & (output_options(ARRAY_C_IDX)))
% Three output collumns:
table_out = table(array_a, array_b, array_c);
end
end
How can I get this 'table_out' with different number of collumns depending of the function input arguments?
  댓글 수: 3
Nycholas Maia
Nycholas Maia 2019년 1월 7일
Hey Geoff,
"are all input column names of the format 'array_a', 'array_b', 'array_c', etc. or can they be something else?"
-> Response: They can be something else
Do you really need to check to see if the input parameters match on these column names or can you just assume that whatever the user is passing in is valid?
-> I would like to show a warninng/error message if the user wrote a invalid collumn name
Where will the data for each column come from? I suspect that it won't be like the dummy array content you have above...
-> In real-life, inside this function a get a real data from a text file and I want to output a custom table collumns
Do you have any solution?
Nycholas Maia
Nycholas Maia 2019년 1월 7일
Let me try a different example to be more clear: People attributes
function table_out = dynamic_table(varargin)
% Set the valid function input arguments:
input_options = {'Name', 'Age', 'Weigth'};
% JUST FOR THIS EXAMPLE, I will set a dummy data for each array.
% BUT in real-life I got a real data from text file and database parse
% Another difference: In real-life I have a lot of collumns, and not only 3;
% Set a dummy array content:
name = {'Joe'; 'Elisa'; 'John'};
age = [30; 10; 25];
weigth = [100; 150; 180];
%% Construct a dynamic conditional output table:
if('function input argment is only (1) Name')
% Only one output collumn:
table_out = table(name);
elseif ('function input argment is (1) Name and (2) Age')
% Two output collumns:
table_out = table(name, age);
elseif ('function input argment is (1) Name and (2) Age and (3) Weigth')
% Three output collumns:
table_out = table(name, age, weigth);
end
end
Running this function, the output should be like:
myTable = dynamic_table('Name')
| Name |
'Joe'
'Elisa'
'John'
Second option:
myTable = dynamic_table('Name', 'Age')
| Name | Age |
'Joe' 30
'Elisa' 10
'John' 25
The last option:
myTable = dynamic_table('Name', 'Age', 'Weigth')
| Name | Age | Weigth |
'Joe' 30 100
'Elisa' 10 150
'John' 25 180
Anyone can give a solution for this?
Thanks!

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

답변 (0개)

카테고리

Help CenterFile Exchange에서 Rotating Machinery에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by