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

91 vues (au cours des 30 derniers jours)
Ishan le 22 Avr 2023
Commenté : Dyuman Joshi le 22 Avr 2023
% 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;
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 commentaireAfficher AucuneMasquer Aucune
Image Analyst le 22 Avr 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:

Connectez-vous pour commenter.

### Réponses (2)

Dyuman Joshi le 22 Avr 2023
Modifié(e) : Dyuman Joshi le 22 Avr 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 commentairesAfficher 1 commentaire plus ancienMasquer 1 commentaire plus ancien
Dyuman Joshi le 22 Avr 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.

Connectez-vous pour commenter.

dpb le 22 Avr 2023
...
...
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
...
and all will work as is...
##### 0 commentairesAfficher -1 commentaires plus anciensMasquer -1 commentaires plus anciens

Connectez-vous pour commenter.

### Catégories

En savoir plus sur Standard File Formats dans Help Center et File Exchange

R2022b

### Community Treasure Hunt

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

Start Hunting!