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

85 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.
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));
sum_latest = sum(data1.^2, [1,2]);
difference = abs(sum_latest - sum_last);
if (difference < 1e-5)
convergence = true;
sum_last = sum_latest;
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)

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 = 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.
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.

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
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
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...




Community Treasure Hunt

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

Start Hunting!