"Error using textscan Invalid file identifier. Use fopen to generate a valid file identifier." - why am i getting this error?

조회 수: 6 (최근 30일)
% Read the data from the file
fid = fopen('gc-data-2.txt', 'r');
data = textscan(fid, '%f %s %f');
Error using textscan
Invalid file identifier. Use fopen to generate a valid file identifier.
fclose(fid);
% 1.Identify the lines that contain data
ind = regexp(data{1}, 'Data');
ind = find(~cellfun(@isempty, ind));
% 2.Get the number of data points
npoints = length(ind);
% Read up to the data
x = data{1}(1:ind(1)-1);
y = data{2}(1:ind(1)-1);
% 3. Initialize the data vectors
time = zeros(npoints, 1);
signal = zeros(npoints, 1);
% Read in the data
for i = 1:npoints
time(i) = data{1}(ind(i));
signal(i) = data{2}(ind(i));
end
% 4.Plot the data
figure
plot(time, signal)
xlabel('Time (s)')
ylabel('Signal')
title('data')
% Correct for non-zero baseline
signal = signal - mean(signal);
% 5.Implementing a fitting procedure using a decaying gaussian function
% Define the decaying gaussian function
gauss = @(p, x) p(1)*exp(-((x - p(2))./p(3)).^2) + p(4);
% Define the objective function
obj = @(p) sum((gauss(p, time) - signal).^2);
% Define the initial guess for the parameters
p0 = [1, mean(time), std(time), 0];
% Optimize the parameters
p = fminsearch(obj, p0);
% 6. implementing a fitting procedure with two peaks and plot the fitting function with an initial guess for each parameter
% Define the function that adds two decaying gaussian functions
gauss2 = @(p, x) p(1)*exp(-((x - p(2))./p(3)).^2) + p(4) + p(5)*exp(-((x - p(6))./p(7)).^2) + p(8);
% 7. Plot fitting function with an initial guess for each parameter
% First we define Define the initial guess for the parameters
p0 = [1, mean(time), std(time), 0, 1, mean(time), std(time), 0];
% Plot the fitting function with the initial guess for the parameters
figure
plot(time, gauss2(p0, time))
xlabel('Time (s)')
ylabel('Signal')
title('fitting function with initial guess')
% 8.using nonlinear fitting to get the parameters that best fit our data, and ploting the fit on the graph
% Optimize the parameters
p2 = lsqnonlin(obj2, p0);
% Plot the fit on the graph
figure
plot(time, signal)
hold on
plot(time, gauss2(p2, time))
xlabel('Time (s)')
ylabel('Signal')
% 9.extracting out the two peaks and integrating the areas
% Extract the two peaks
peak1 = gauss(p2(1:4), time);
peak2 = gauss(p2(5:8), time);
% Display the two peaks extracted
figure
plot(time, peak1)
hold on
plot(time, peak2)
xlabel('Time (s)')
ylabel('Signal')
legend('Peak 1', 'Peak 2')
title('the two extracted peaks')
% Integrate the areas
area1 = trapz(time, peak1);
area2 = trapz(time, peak2);
% Display the integrated areas
fprintf('Integrated area 1: %f\n', area1)
fprintf('Integrated area 2: %f\n', area2)
% 10.Compute the relative amounts
rel1 = area1/(area1 + area2);
rel2 = area2/(area1 + area2);
% Display the relative amounts
fprintf('Relative amount 1: %f\n', rel1)
fprintf('Relative amount 2: %f\n', rel2)
  댓글 수: 4
Voss
Voss 2024년 9월 3일
@Jeslyn: That error happens when attempting to access an element beyond the size of the array.
Example:
x = [10 20 30]
x = 1x3
10 20 30
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
x(1) % ok
ans = 10
x(2) % ok
ans = 20
x(3) % ok
ans = 30
x(4) % error: no x(4), x has only 3 elements
Index exceeds the number of array elements. Index must not exceed 3.
Walter Roberson
Walter Roberson 2024년 9월 3일
ind = regexp(data{1}, 'Data');
The default output from regexp is a numeric vector of indices of matches.
ind = find(~cellfun(@isempty, ind));
You cannot cellfun() over a numeric vector.
But
data = textscan(fid, '%f %s %f');
textscan() returns a cell array with one entry for each un-suppressed format specifier. The cell array entries have datatype according to the format specifier. The %f format will result in the cell array entry being an array of double.
You then try to regexp() that array of doubles, which is likely to fail.

댓글을 달려면 로그인하십시오.

채택된 답변

Cris LaPierre
Cris LaPierre 2023년 1월 12일
편집: Cris LaPierre 2023년 1월 12일
The most likely reason is because the file you are trying to open does not exist. Check that you have spelled the file name correctly, and that the file is either in your current folder, or in a folder that has been added to your MATLAB Path.
You many need to include the path to the file (relative or absolute) along with the filename to fopen. You may want to consider using fullfile to construct that.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Descriptive Statistics에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by