Table: Add collumn with conditional

1 view (last 30 days)
Nycholas Maia
Nycholas Maia on 7 Jan 2019
Commented: Nycholas Maia on 7 Jan 2019
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 Comments
Nycholas Maia
Nycholas Maia on 7 Jan 2019
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 on 7 Jan 2019
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!

Sign in to comment.

Answers (0)

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!