plsrectify the error in my code

Error in MI_fitting (line 6)
field = mat1(:,1); % safer extraction, This is the error observed in my Matlab Code.
clc;
clear;
close all;
mat1 = readtable('mat1.txt');
Error using readtable (line 19)
Unable to find or open 'mat1.txt'. Check the path and filename or file permissions.
field = mat1(:,1); % safer extraction
Z = mat1(:,2);
plot(field,Z,'bo');
hold on;
lb=[50,201,5e9,0.0001,8.1e-6,0.001,50];
ub=[100,419,100e9,1,151e-6,21,71];
MI = lsqcurvefit(@impedance_test1,[85,391.29,20e9,5,16e-6,5,59.03],field,Z,lb,ub);
[fitdata,Mr] = impedance_test1(MI,field);
plot(field, fitdata,'-r','LineWidth',2);
xlabel('Field');
ylabel('MI');
legend('mat1','fitting')
title('MI vs Field')
function[modZ,Mr]=impedance_test1(var,field)
M=var(1);Ha=var(2);sig2=var(3);alpha=var(4);D=var(5);si=var(6);teta=var(7);
%Define constants
g=((2.2).*(10e5));
w=((2).*(10e6));
M0=((4).*(pi).*(10e-7));
c=3*(10e10);
l=3;
% Define function.
wm=g.*4.*(pi).*M;
w1=g.*((Ha.*(cosd(teta-si)).^2)+((field).*(sind(teta))));
w2=g.*((Ha.*(cosd(2.*(teta-si))))+((field).*(sind(teta))));
%Relative permeability
Mr=(wm.*(wm+w1-(1i.*(alpha).*w)))./(((wm+w1-(1i.*(alpha).*w)).*(w2-(1i.*(alpha).*w)))-(w.^2));
Mr=abs(Mr);
Mef=Mr.*M0;
T2=c./(sqrt(2.*(pi).*(sig2).*w));
Z=(((1-1i).*l).*(((sqrt(Mef+1)).*((sind(teta)).^2))+(cosd(teta)).^2)./((pi).*(sig2).*D.*T2));
modZ=((abs(Z)./(2.34401312390370e-09))-1).*100;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end
0 121.58696
0.8 117.91178
1.6 114.23316
2.4 112.21164
3.2 110.54767
4 108.67398
4.8 105.74483
5.6 102.94633
6.4 100.21659
7.2 97.78595
8 95.7438
8.8 93.43005
9.6 91.32946
10.4 88.97446
11.2 86.53694
12 84.33321
12.8 82.17761
13.6 80.04263
14.4 77.99017
15.2 75.96177
16 73.95056
20 67.38405
24 59.11919
28 51.67944
32 45.06481
36 39.36466
40 34.35555
44 29.97903
48 26.08382
52 22.6321
56 19.59982
60 16.87008
64 14.38443
68 12.16007
72 10.14886
76 8.29924
80 6.62495
84 4.54155
88 3.62017
92 2.31375
96 1.12421

1 Comment

Try to make a MWE so we can run your code without any other dependencies and can reproduce your issue. The best way to do this is to use the code section in the editor and use the run button. That way you can make sure we will see the same error message as you do.

Sign in to comment.

Answers (1)

The error occurs because READTABLE returns a table object, and you cannot extract the content from a table using parentheses like mat1(:,1). With tables, you need to use curly brackets {} or dot notation if you want the content e.g. as a numeric array. This is explained here:
Alternatively you could skip the table entirely and simply import the data as a numeric matrx using e.g. READMATRIX.
With the correct table indexing the code works without error:
% Read table:
mat1 = readtable('mat1.txt');
% Extract data as numeric arrays:
field = mat1{:,1};
Z = mat1{:,2};
% Plot experimental data:
plot(field, Z, 'bo');
hold on;
% Boundaries for fit
lb = [50, 201, 5e9, 0.0001, 8.1e-6, 0.001, 50];
ub = [100, 419, 100e9, 1, 151e-6, 21, 71];
% Fitting:
MI = lsqcurvefit(@impedance_test1, [85, 391.29, 20e9, 5, 16e-6, 5, 59.03], field, Z, lb, ub);
Local minimum possible. lsqcurvefit stopped because the final change in the sum of squares relative to its initial value is less than the value of the function tolerance.
% Generate fitted data:
[fitdata, Mr] = impedance_test1(MI, field);
% Plot fit:
plot(field, fitdata, '-r', 'LineWidth', 2);
% Plot settings:
xlabel('Field');
ylabel('MI');
legend('mat1', 'fitting');
title('MI vs Field');
% Fitting function:
function [modZ, Mr] = impedance_test1(var, field)
M = var(1);
Ha = var(2);
sig2 = var(3);
alpha = var(4);
D = var(5);
si = var(6);
teta = var(7);
% Define constants
g = 2.2 * 1e5;
w = 2 * 1e6;
M0 = 4 * pi * 1e-7;
c = 3 * 1e10;
l = 3;
% Intermediate calculations:
wm = g * 4 * pi * M;
w1 = g * (Ha * (cosd(teta - si)).^2 + field * sind(teta));
w2 = g * (Ha * (cosd(2 * (teta - si))) + field * sind(teta));
% Relative permeability:
Mr = (wm .* (wm + w1 - 1i * alpha * w)) ./ ...
((wm + w1 - 1i * alpha * w) .* (w2 - 1i * alpha * w) - w^2);
Mr = abs(Mr);
Mef = Mr * M0;
T2 = c / sqrt(2 * pi * sig2 * w);
Z = ((1 - 1i) * l) .* ((sqrt(Mef + 1) .* (sind(teta).^2)) + (cosd(teta).^2)) ...
./ (pi * sig2 * D * T2);
modZ = ((abs(Z) / 2.34401312390370e-09) - 1) * 100;
end

Products

Release

R2024a

Asked:

about 13 hours ago

Commented:

Rik
about 13 hours ago

Community Treasure Hunt

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

Start Hunting!