Inputting a table into a function

36 views (last 30 days)
Sebastiano Zuddas
Sebastiano Zuddas on 15 Dec 2021
I want to create a function that would be used to look through a table to convert all elements from 'true'/'false' to 1/0.
The function would take a table as an input, check the column to see whether a character is being returned.
If it doesn't, it increments columns.
If the condition is met then it would loop through turning all elements in that column into corresponding 1/0 values, using if statements.
It then increements columns.
I've come up with this piece of code so far:
function [outoput] = binaryconv(tableinputted)
class(tableinputted{9,9})
for i =1:width(tableinputted)
if class(tableinputted{i,5}) ~= "char" %look 5 rows down the ith column, if char isn't returned
i=i+1; %increment column
else %otherwise go through process
for j=1:height(tableinputted)%loop through the height of the table
if tableinputted{i,j}=="false" %if given something that reads 'false'
tableinputted{i,j}= 0; %make the value zero
elseif tableinputted{i,j}=="true" %if given the value true
tableinputted{i,j}=1;%give the value one
end
j=j+1
end
i=i+1
end
return;
end
end
However, when I run this the tables aren't amended.
Any pointers of how to go about this?
Thanks in advance!
  1 Comment
Voss
Voss on 16 Dec 2021
Edited: Voss on 16 Dec 2021
The function returns a variable outoput which is never defined in the function. Most likely you intend to return the variable tableinputted instead, which is the amended input table.
However, there are at least a few other problems here, I think. (I can't run your code as-is because my MATLAB version is old and doesn't support String variables.)
1) I'm pretty sure your i and j are swapped whenever you index the table (j should be first - MATLAB is row first, column second indexing), so use tableinputted{j,i} instead of tableinputted{i,j} throughout and tableinputted{5,i} toward the top.
2) You don't want to return after the first time through the for i loop, do you?
3) Incrementing loop iterator variables at the end of the loop has no effect, so you can remove the i=i+1 and j=j+1 lines.
I think the approach of the code is good. I mean, the algorithm you came up with to modify a table in that specific way should work perfectly well, once those few items I mention are corrected.

Sign in to comment.

Answers (1)

Siddharth Bhutiya
Siddharth Bhutiya on 5 Jan 2022
I would suggest using strings to represent the text in your table rather than char. Also I am not sure how you are working with chars right now because 'true' is a 4x1 char vector and 'false' is a 5x1 char vector, so you cannot have a char variable that has a mix of true and false, since that would be an error
>> ['true';'false']
Error using vertcat
Dimensions of arrays being concatenated are not consistent.
Now once you convert your text to a string, there might be couple of ways to do this. You do not need to use a for loop to do this, you can simply use indexing with logical condition to do this. For example, here I have a table where Var2 and Var4 are my string variables
>> t
t =
10×4 table
Var1 Var2 Var3 Var4
____ ______ _______ _______
1 "true" 1 day "true"
2 "true" 2 days "false"
3 "true" 3 days "false"
4 "true" 4 days "true"
5 "true" 5 days "false"
6 "true" 6 days "false"
7 "true" 7 days "true"
8 "true" 8 days "false"
9 "true" 9 days "true"
10 "true" 10 days "false"
Now first of all, you can use "vartype" to indentify vars of a given type (in our case "string")
>> t(:,vartype("string"))
ans =
10×2 table
Var2 Var4
______ _______
"true" "true"
"true" "false"
"true" "false"
"true" "true"
"true" "false"
"true" "false"
"true" "true"
"true" "false"
"true" "true"
"true" "false"
Since we want to replace the contents of all the variables, we should use {} brace indexing to get the contents and then replace "true" with "1" and "false" with "0" as follows:
>> t{:,vartype("string")}
ans =
10×2 string array
"true" "true"
"true" "false"
"true" "false"
"true" "true"
"true" "false"
"true" "false"
"true" "true"
"true" "false"
"true" "true"
"true" "false"
>> t{:,vartype("string")} = replace(t{:,vartype("string")},["true","false"],["1","0"])
t =
10×4 table
Var1 Var2 Var3 Var4
____ ____ _______ ____
1 "1" 1 day "1"
2 "1" 2 days "0"
3 "1" 3 days "0"
4 "1" 4 days "1"
5 "1" 5 days "0"
6 "1" 6 days "0"
7 "1" 7 days "1"
8 "1" 8 days "0"
9 "1" 9 days "1"
10 "1" 10 days "0"
Here the variable type still remains string, if you want it to be numbers, you can use convertvars to convert the table variables into doubles
>> t = convertvars(t,vartype("string"),'double')
t =
10×4 table
Var1 Var2 Var3 Var4
____ ____ _______ ____
1 1 1 day 1
2 1 2 days 0
3 1 3 days 0
4 1 4 days 1
5 1 5 days 0
6 1 6 days 0
7 1 7 days 1
8 1 8 days 0
9 1 9 days 1
10 1 10 days 0

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!