I am getting "Conversion to logical from table is not possible." error, how do you solve it?

75 views (last 30 days)
% This program focuses on a grid that contains mesh points. An electric
% potential is applied to the edges of the grid, causing a corresponding
% potential to be induced within the inner mesh channels. Using the
% distance between the nodes as a known parameter, the program calculates
% the voltage of the inner nodes by averaging the voltage values of the
% 4 surrounding nodes.
% Imported data - data1.csv = This is a file which contains a logical
% matrix. A logical '1' is present at the unknown interior nodes and a
% logical '0' is present on the boundary of the grid for which the values
% are known.
clear;
sum_last = 0;
sum_latest = 0;
difference = 0;
convergence = 0;
data1 = readtable('data1.csv');
data2 = readtable('data2.csv');
while ~convergence
for i = 2 : size(data1, 1) - 1
for j = 2 : size(data1, 1) - 1
if (data1(i, j))
data2(i, j) = 0.25 * (data2(i, j+1) + data2(i+1, j) + ...
data2(i, j-1) + data2(i-1, j));
end
end
end
sum_latest = sum(data1.^2, [1,2]);
difference = abs(sum_latest - sum_last);
if (difference < 1e-5)
convergence = true;
else
sum_last = sum_latest;
end
end
writetable(data2, 'data2.csv');
  1 Comment
Image Analyst
Image Analyst on 22 Apr 2023
Unfortunately you forgot to attach the two CSV files.
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:

Sign in to comment.

Answers (2)

dpb
dpb on 22 Apr 2023
...
data1 = readtable('data1.csv');
data2 = readtable('data2.csv');
...
You read a csv file as a table; the two variables are thus of class table, not doubles nor logicals; you need to reference the variable in the table, not the table itself.
You could do this in one of two ways; use the variable name with the "dot" notation as
data1.Var1(i,j)
where it is presumed the default name and that your csv file doesn't have a header column providing any other name; just contains the data values, or with "the curlies" as
data1{i,j}
where the {} act to retrieve the underlying array inside the table.
For such a case as this, however, it doesn't really make sense to use a table at all; they're really useful for holding disparate data types to be addressed by a variable name; there isn't so much real value here for simply numeric values. Instead just read the data itself with
data1 = readmatrix('data1.csv');
data2 = readmatrix('data2.csv');
...
and all will work as is...
  1 Comment
Jose
Jose on 5 Dec 2023
Thank you for the adviece on the table as I have project where I had to import a csv file and imported it as a table, thanks to what you explained regarsing it being in a class of tables I was able to refrence the matrix in the csv file.

Sign in to comment.


Dyuman Joshi
Dyuman Joshi on 22 Apr 2023
Edited: Dyuman Joshi on 22 Apr 2023
The input data to writetable() is supposed to be a table, which is not in this case. Use readmatrix and writematrix instead.
And you need to convert your data, specifically data2, to numeric data from logical, as when you assign a non-logical value to an element in a logical array, depending upon it's value, it's stored as either 0 or 1.
Choose either single or double data type.
y = logical(randi(2,4,5)-1)
y = 4×5 logical array
0 1 0 0 0 0 1 1 1 0 1 0 1 1 0 0 0 0 1 1
y(2,2)=0.25*(y(1,2)+y(3,2)+y(2,3)+y(2,1))
y = 4×5 logical array
0 1 0 0 0 0 1 1 1 0 1 0 1 1 0 0 0 0 1 1
%Expected value was 0.5
Also, you can define "sum_latest" outside the loop as it is not varying with the loop.
  2 Comments
dpb
dpb on 22 Apr 2023
Not clear exactly how OP got the logical class assigned to the data; it's not shown explicitly in the posted code unless the input file contains the true, false keywords; a series of 0,1 in a csv file would be interpreted as doubles by default.
I was thinking promotion would go the other way; good catch w/ a demo that it does the other when refer to a defined array element-by-element.
On the invariant sum; unless that is changed to compute the sum over data2, the result will never change and his code will be in an infinite loop...
Dyuman Joshi
Dyuman Joshi on 22 Apr 2023
Yes, it is not clear how the data comes out assigned as logical, when readtable(), as you mentioned, imports data as table() data type.
Regarding the sum, if the sum_latest is 0 then the whole loop will break in the 1st iteration. If sum_latest is not 0, then sum_last will be equal to sum_latest and thus the loop will break in the 2nd iteration.

Sign in to comment.

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!