Matlab Function Call From The Script: Subscripted assignment dimension mismatch - matlab

MSIZE=100
reftype=0
ident(2,2)=0
fid1= fopen('MSTRPIN.txt','rt');
if fid1 < 0
fprintf('error opening file\n');
return;
end
fid2= fopen('MYPULDAT.txt','w+t');
if fid2 < 0
fprintf('error opening file2\n');
return;
end
PI=4.0*atan(1.0);
V0=2.997925e8;
V02=V0*V0;
EPS=1.0/(V02*4.e-7*PI);
FAC=1./(2.*PI*EPS);
CMTM=2.54E-5;
for i=1:MSIZE
for j=1:MSIZE
ident(i,j)=0.0;
ident(i,i)=1.0;
end
end
line_number = 1;
oneline{line_number} = fgetl(fid1);
while ischar(oneline{line_number})
line_number = line_number + 1;
oneline{line_number} = fgets(fid1);
end
fclose(fid1);
for i=1:line_number-1
Data_1(i,1) = sscanf(oneline{i}(1:3),'%f,');
end
n = Data_1(1,1);
ncdiv= Data_1(2,1);
w = Data_1(3,1);
sep = Data_1(4,1);
t = Data_1(5,1);
er = Data_1(6,1);
w =w*CMTM;
sep =sep*CMTM;
t =t*CMTM;
DELTA=w/double(ncdiv)
%%%%%%%%%Without Dielectric*%%%%%%%%%%%%%%%
%%%%%%%%%Fill A1%%%%%%%%%%%%%%%%%%%%%%%%%%%
dist=0.0;
for i=1:ncdiv
A(1,i)=PHI(dist)
A(i,1)=A(1,i);
dist=dist+DELTA;
end
The script above is calling PHI(.) function:
function ret= PHI( D )
global FAC ;
global DELTA;
global t;
DOW=2.0*D./DELTA
TOW=4.0*t./DELTA
DOWM=DOW-1.0
DOWP=DOW+1
if(D==0)
ret=FAC*(0.5*log(1.0+TOW*TOW)+TOW*atan(1.0./TOW))
else
ret=DOWM*log(DOWM)DOWP*log(DOWP)-0.5*DOWM*log(DOWM^2+TOW^2)+
0.5*DOWP*log(DOWP^2+TOW^2)-TOW*(atan(DOWM/TOW)-atan(DOWP/TOW))
ret=FAC/2.0
end
end
But when the the script run an error occurs:
Subscripted assignment dimension mismatch.
Error in widesefor (line 62)
A(1,i)=PHI(dist)
When I assign a value manually to the D input variable in the function no error occurs. What is the reason?

In PHI function, there are global variable declerations: FAC, DELTA , t.
To share these variables with a script file, same variables also must be declared
as "GLOBAL" in the script as well as in the function definition.
In the error situation, DELTA and t is not initialized properly in the function
file because of missing 'GLOBAL' declaration in the script file.

Related

Keep getting invalid expression errors

I have this code, which is taken as an example from learning material.
function sum_out = sum_of_squares(2)
sum_out = 0;
for i = 1:n;
sum_out + i*i;
end
end
However I cannot run it because I get this error message:
>> sum_of_squares
Error: File: sum_of_squares.m Line: 2 Column: 35
Invalid expression. Check for missing multiplication operator, missing or
unbalanced delimiters, or other syntax error. To construct matrices, use
brackets instead of parentheses.
Where is the problem?
The expression between parentheses in the function definition line should be the name of the parameter to the function, not a value. I think you mean:
function sum_out = sum_of_squares(n)
sum_out = 0;
for i = 1:n;
sum_out + i*i;
end
end
Then, when you execute sum_of_squares(2), inside the function the variable n will have the value 2.
More about defining functions in the doc.
After the answer above i corrected it as such
n=2
function sum_out = sum_of_squares(n)
sum_out = 0;
for i = 1:n;
sum_out + i*i;
end
end

What could be wrong with the file path in the following code?

I understand that this could be due to a wrong file path but I have tried all the ways to correct it and still ending up with the same error. The code is given below.
clc
clear all
fid=fopen('ECresult.txt','w');
for p=1:2;
% textFilename{p} = fullfile('/ccc/Desktop/Lalitha-18/EO',['EO' num2str(p)'.txt']);
textFilename{p} = fullfile('C:/Users/Biswajit/Desktop/Biswajit_nimmy',['LC1EC1' num2str(p) '.txt']);
id{p} = fopen(textFilename{p},'rt');
textdata{p} = textscan(id{p},'%s%s');
fclose(id{p});
p
X=load(['EC' num2str(p) '.txt']);
N=length(X)
m=2;
counter1=0; counter2=0;
k=0.2;
r = k*std(X);
i=1;j=1;
while i<= N-m
Y=[X(i:i+m-1)];
while j<=N-m
Z=[X(j:j+m-1)];
d1=abs(Y-Z);
if d1<=r
counter1=counter1 + 1;
end
j=j+1;
end
IC(i) = counter1/(N-m);
counter1=0;j=1;
i=i+1;
end
i=1;
m=m+1;
while i <= N-m
Y = [X(i:i+m-1)];
while j<= (N-m)
Z = [X(j:j+m-1)];
d2 = abs(Y-Z);
if d2<=r
counter2=counter2 +1;
end
j=j+1;
end
ID(i)=counter2/(N-m);
counter2=0; j=1;
i = i+1;
end
Entropy=0;
i=1;
while i<=N-m
Ratio=IC(i)/ID(i);
Entropy=Entropy +log (Ratio);
i = i+1;
end
Approxntropy(p) = Entropy/(N-m);
fid=fopen('EO.txt','w');
fprintf(fid, '%5.6f\r\n', Approxntropy);
end
The exact error message is as below
"
Error using textscan
Invalid file identifier. Use fopen to generate a valid file identifier.
Error in Apensdploop (line 13)
textdata{p} = textscan(id{p},'%s%s');
"
Please advise.
Your error message means that fopen was unable to open the file. In this case, it returns -1. Since you didn't catch the error, you passed -1 to textscan which then complains. Therefore, before passing id{p} to textscan, you should check if it is -1, which means an error.
What you are passing to fopen is
fullfile('C:/Users/Biswajit/Desktop/Biswajit_nimmy',['LC1EC1' num2str(p) '.txt']);
which for p = 1 becomes
'C:/Users/Biswajit/Desktop/Biswajit_nimmyLC1EC11.txt'
Looks like a slash is missing to me. Try
fullfile('C:/Users/Biswajit/Desktop/Biswajit_nimmy/',['LC1EC1' num2str(p) '.txt']);
instead?
Otherwise, set a breakpoint at the fopen like and look at the content of textFilename{p}. Then you will see if it contains the correct path and file name or not.

How can I declare 2-dimentional array as parameter in function In MATLAB?

I have a function that gets a matrix and do some operations on it.
but I do not know how to declare function's parameter .
this is my function Code:
function D=Shirinkage(a)
D(1,:)=a(1,:);%this is X1
for i=2:4
D(i,4)=0;
for j=1:3
D(i,j)=1/2*(a(1,j)+a(i,j));
D(i,4)=D(i,j)^2 + D(i,4); %object function
end
end
end
I tried a(4,4) instead of a (parameter of the function),but the error does not appear.
Error:
??? Input argument "a" is undefined.
Error in ==> Shirinkage at 3
D(1,:)=a(1,:);%this is X1
also I want to declare D correctly.
appreciating any help.
Edited:
i call my function from a script file , in this way:
I have a 2-dimention array(matrix) its size is : 4*4 and its name is A.
i want my function gets this matrix and do the operation on it and the result can be saved again in it.
A=Shirinkage(A)
e.x. A has this values:
A=[1,2,3,4;2,3,4,5;5,6,7,8;1,2,3,4]
The function you created is working fine. The only recommendation I have to pre-allocate the size of D as it varies in each iteration in your current code.
function D = Shirinkage(a)
D = zeros(size(a));
D(1,:) = a(1,:); %this is X1
for i = 2:4
D(i,4) = 0;
for j = 1:3
D(i,j) = 0.5*(a(1,j) + a(i,j));
D(i,4) = D(i,4) + D(i,j)^2; %object function
end
end
end
The function was called from command window by using the same matrix you have used and it gave the following output.
The error you have posted says that the function hasn't received the argument a. If your script and the function are in the same MATLAB path, this should work perfectly.

Check Position with for loop, not enough input arguments - Matlab

I made a simple function that loops between the rows and columns of an array using for loops. The loop is part of a function named checktakentest (Since I'm testing this method atm). I keep getting the error that there aren't enough input arguments.
function [spotTaken] = checktakentest(tttArray)
for h = 1:3
if tttArray(h,j) == 1
%Is spot is taken, break loop
spotTaken = 1; break;
else
spotTaken = 0;
end
for j=1:3
if tttArray(h,j) == 1
spotTaken = 1; break;
else
spotTaken = 0;
end
end
end
I tried also defining h and j previously as follows
h = [1,2,3];
j = [1,2,3];
Note that tttArray is a global variable defined in another function and its array values change in that function. A spot taken is 1, empty is 0. What arguments should I pass to the function and how do I know which ones to pass since this has been a recurring problem for me? A simple explanation would be appreciated. Note that I call the function via
checktakentest(tttArray)
Just remove the first if clause - at that point you don't have j initialized to a value, so you can't use it, yet:
function [spotTaken] = checktakentest(tttArray)
for h = 1:3
for j=1:3
if tttArray(h,j) == 1
spotTaken = 1; break;
else
spotTaken = 0;
end
end
end
If you call your function like this: checktakentest(tttArray) with tttArray beeing a mxn-matrix with m>2 and n>2 you should not get an error.
If you call it like this: checktakentest you will get the error you described (not enough input arguments).

Object array implemenation in MATLAB OOP

Suppose that we have this class in MATLAB R2014b :
classdef Pre
properties
Value
end
methods
function obj = Pre(F)
if nargin ~= 0
m = size(F,1);
n = size(F,2);
obj(m,n) = Pre;
for i = 1:m
for j = 1:n
obj(i,j).Value = F(i,j);
end
end
end
end
end
end
1) If we erase if nargin~=0 from this code we have this error :
Error using Pre (line 13)
Not enough input arguments.
Error in Pre (line 15)
obj(m,n) = Pre;
Why? I think this is only checking for number of input arguments !
2) what is obj(m,n) = Pre;? what this line is doing in this code? This is for pre-allocating but how this line can do that?
I checked this class with this syntax: az = Pre([2 3 5;5 3 0])
1) In the line obj(m,n) = Pre; you call Pre without any input argument, so the variable F doesn't exist in that function call. It would therefore fail to do size(F,1) etc. MATLAB prevents this by throwing the error not enough input arguments. As long as you want the possibility to call Pre with or without input arguments, you need to check if an argument exists or not.
2) This does a pre-allocation. By creating an empty Pre at the location (m,n) of obj, MATLAB will initialize obj as matrix of type Pre and size mxn. (You can verify this for normal variables by typing a(2,2) = 0 in the MATLAB console - it will return a 2-by-2 matrix of zeros).