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.
Related
I want to do this because otherwise I run out of memory to save the values in a matrix, as the dimensions of the matrix increases.
For example,
for a=1:10000
for b=1:10000
M(a,b)=rand;
end
end
Here, the system runs out of memory as it can't store a 10000x10000 matrix.
So, I want to keep writing the (a,b)'th value inside the for loops into the (a,b)'th cell of a csv file (or an xls file, whatever). How do I do that?
I want something like this:
for a=1:10000
for b=1:10000
xlswrite1('file.xls',a,b,rand); % better if I can get a solution with csvwrite, as I prefer CSVs to work with
end
end
But this obviously doesn't work. The error it gives is:
Error using evalin
Undefined function or variable 'Excel'.
Error in xlswrite1 (line 2)
Excel=evalin('caller','Excel');
Then when I add the following lines according to this article,
Excel = actxserver ('Excel.Application');
File='datafile.xls';
if ~exist(File,'file')
ExcelWorkbook = Excel.workbooks.Add;
ExcelWorkbook.SaveAs(File,1);
ExcelWorkbook.Close(false);
it gives this error:
Error using xlswrite1 (line 82)
Range argument must a string of Excel A1 notation.
Can anyone please suggest a simple solution? I can't find any from my online search so far.
You can do this using dlmwrite. But I suggest you to write row by row instead of element by element.
N = 100 ;
M = zeros(N,N) ;
filename = 'test.csv';
for a=1:N
for b=1:N
M(a,b)=rand;
end
dlmwrite(filename,M(a,:),'-append')
end
If you don't want to make a matrix M, then make rows and write them to your file:
N = 100 ;
M = zeros(1,N) ;
filename = 'test.csv';
for a=1:N
for b=1:N
M(1,b)=rand;
end
dlmwrite(filename,M(1,:),'-append')
end
I suggest using fprintf:
N = 100 ;
M = zeros(1,N) ;
filename1 = 'test1.csv';
t1 = tic ;
for a=1:N
for b=1:N
M(1,b)=rand;
end
dlmwrite(filename1,M,'-append')
end
t1 = toc(t1) ;
filename2 = 'test2.csv';
M = zeros(1,N) ;
t2 = tic ;
fid = fopen(filename2,'w') ;
for a=1:N
for b=1:N
M(1,b)=rand;
end
fprintf(fid, [repmat(' %f ', 1, N) '\n'], M') ;
end
fclose(fid) ;
t2 = toc(t2) ;
fprintf('time taken using dlmwrite:%f\n',t1)
fprintf('time taken using fprintf:%f\n',t2)
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.
I have an index file (called runnumber_odour.txt) that looks like this:
run00001.txt ptol
run00002.txt cdeg
run00003.txt adef
run00004.txt adfg
I need some way of loading this in to a matrix in matlab, such that I can search through the second column to find one of those strings, load the corresponding file and do some data analysis with it. (i.e. if I search for "ptol", it should load run00001.txt and analyse the data in that file).
I've tried this:
clear; clc ;
% load index file - runnumber_odour.txt
runnumber_odour = fopen('Runnumber_odour.txt','r');
count = 1;
lines2skip = 0;
while ~feof(runnumber_odour)
runnumber_odourmat = zeros(817,2);
if count <= lines2skip
count = count+1;
[~] = fgets(runnumber_odour); % throw away unwanted line
continue;
else
line = strcat(fgets(runnumber_odour));
runnumber_odourmat = [runnumber_odourmat ;cell2mat(textscan(line, '%f')).'];
count = count +1;
end
end
runnumber_odourmat
But that just produces a 817 by 2 matrix of zeros (i.e. not writing to the matrix), but without the line runnumber_odourmat = zeros(817,2); I get the error "undefined function or variable 'runnumber_odourmat'.
I have also tried this with strtrim instead of strcat but that also doesn't work, with the same problem.
So, how do I load that file in to a matrix in matlab?
You can do all of this pretty easily using a Map object so you will not have to do any searching or anything like that. Your second column will be a key to the first column. The code will be as follows
clc; close all; clear all;
fid = fopen('fileList.txt','r'); %# open file for reading
count = 1;
content = {};
lines2skip = 0;
fileMap = containers.Map();
while ~feof(fid)
if count <= lines2skip
count = count+1;
[~] = fgets(fid); % throw away unwanted line
else
line = strtrim(fgets(fid));
parts = regexp(line,' ','split');
if numel(parts) >= 2
fileMap(parts{2}) = parts{1};
end
count = count +1;
end
end
fclose(fid);
fileName = fileMap('ptol')
% do what you need to do with this filename
This will provide for quick access to any element
You can then do what was described in the previous question you had asked, with the answer I provided.
I have one program that has function and the problem, return value, it has too many output.
Like exempley: y = text the answer comes up
Error in text (line 2)
if nargin == 0
Output argument "array" (and maybe others) not assigned during call to "
C:\Users\name\Documents\MATLAB\text.m>text".
The program text.m reads a txt file that contains a couple of names and numbers like
exemple:
John doughlas 15986
Filip duch 357852
and so on. The program convert them to 15986 Doughlas John and so on.
function array = text(~)
if nargin == 0
dirr = '.';
end
answer = dir(dirr);
k=1;
while k <= length(answer)
if answer(k).isdir
answer(k)=[];
else
filename{k}=answer(k).name;
k=k+1;
end
end
chose=menu( 'choose file',filename);
namn = char(filename(chose));
fid = fopen(namn, 'r');
R = textscan(fid,'%s %s %s');
x=-1;
k=0;
while x <= 24
x = k + 1;
All = [R{3}{x},' ',R{1}{x},' ',R{2}{x}];
disp(All)
k = k + 1;
end
fclose(fid);
Is there anyway to fix the problem without starting over from scratch?
Grateful for all the answers!
You specify the function output argument in the definition, but you don't assign anything to it in the function body.
For example, in
function y = student(j)
your output is y. So you have to assign something to y.
Read more about functions in MATLAB.
Here is a working example.
The first part is to create a function called 'functionA' in a filename 'functionA.m'. Then put the following code inside:
function result = functionA(N,alpha)
result = 5;
return
end
The second part is to create another Matlab file(i.e. upto you to name it) or you can use the Matlab command window even. Then run the following code:
getresult = functionA(100,10);
getresult
After running you get the following answer:
ans =
5
i got error Error using wavread (line 67)
Invalid Wave File. Reason: Cannot open file.
Error in energy_per_frame (line 2) [inpu fs] = wavread(file);
this is the source code for function that call energy_per_frame.
[inpu fs] = wavread(file);
clc;clear;close all;
file = input('Enter name of wav file ', 's');
counter=0;
for test_number = 0:4
for test_number2 = 0:20
counter=counter+1;
reference = dtw_avg(test_number2);
test = energy_per_frame(file);
distance(counter) = dtw(test,reference);
end
This is source code for function energy_per_frame
[inpu fs] = wavread(file);
fn = fs/2;
bins = 512; %useable bins
frame_length = bins;
window = hamming(frame_length);
overlap = .5;
number_of_frames = (length(inpu)/(frame_length)/overlap) - 1;
What is wrong here?
Well, your function definition should look something like this
function [ output_args ] = energy_per_frame( input_args )
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
end
It looks like your missing the function keyword.
However, if the first block is just supposed to be a script then you're making the call to wavread before you actually have the user input for the filename. Also, there is really no need to call clear etc. like that in a function.