当然,我将尝试为你调整和改进代码,同时添加一些可能有助于调试的输出信息,以帮助识别问题。以下是完整修改后的代码:
- function [nx, ny, dx, dy, isPol, lambda, EX, EY, PilotRays] = read_ZBF(filename)
- % 接收一个文件名,返回采样点数(nx,ny),网格距离(dx,dy),偏振状态(isPol),
- % 波长(lambda),电场数据(EX,EY),引导光线的数据(PilotRays)
- % 打开文件
- fid = fopen(filename, 'r');
- if fid == -1
- error('Failed to open the file.');
- end
- % 读取第一部分(文件格式,采样点数以及偏振状态)
- vec = fread(fid, 9, 'int32'); % 假设整数是32位的,根据实际情况可能需要调整
- if numel(vec) ~= 9
- fclose(fid);
- error('Failed to read header data correctly.');
- end
- fileFormat = vec(1);
- nx = vec(2);
- ny = vec(3);
- isPol = vec(4);
- % 打印输出以便调试
- fprintf('File format: %d, nx: %d, ny: %d, isPol: %d\n', fileFormat, nx, ny, isPol);
- % 读取第二部分(根据文件格式)
- if fileFormat == 1
- vec = fread(fid, 20, 'double');
- if numel(vec) < 9
- fclose(fid);
- error('Failed to read second section for file format 1.');
- end
- lambda = vec(9);
- PilotRays.posX = vec(3);
- PilotRays.posY = vec(6);
- PilotRays.rayleighX = vec(4);
- PilotRays.rayleighY = vec(7);
- PilotRays.waistX = vec(5);
- PilotRays.waistY = vec(8);
- else
- vec = fread(fid, 11, 'double');
- if numel(vec) < 5
- fclose(fid);
- error('Failed to read second section for other file format.');
- end
- lambda = vec(5);
- PilotRays.posX = vec(3);
- PilotRays.posY = vec(3);
- PilotRays.rayleighX = vec(4);
- PilotRays.rayleighY = vec(4);
- PilotRays.waistX = vec(6);
- PilotRays.waistY = vec(6);
- end
- dx = vec(1);
- dy = vec(2);
- % 读取电场数据EX
- vecEX = fread(fid, nx * ny * 2, 'double');
- fprintf('Elements read for EX: %d\n', numel(vecEX));
- if numel(vecEX) ~= nx * ny * 2
- fclose(fid);
- error('The data size does not match expected dimensions.');
- end
- % 创建复数表示
- realEX = reshape(vecEX(1:2:end), ny, nx);
- imagEX = reshape(vecEX(2:2:end), ny, nx);
- EX = realEX + 1i * imagEX;
- % 如果光束是偏振的,读取电场数据EY
- if isPol == 1
- vecEY = fread(fid, nx * ny * 2, 'double');
- fprintf('Elements read for EY: %d\n', numel(vecEY));
- if numel(vecEY) ~= nx * ny * 2
- fclose(fid);
- error('The data size does not match expected dimensions for EY.');
- end
- realEY = reshape(vecEY(1:2:end), ny, nx);
- imagEY = reshape(vecEY(2:2:end), ny, nx);
- EY = realEY + 1i * imagEY;
- else
- EY = zeros(ny, nx);
- end
- fclose(fid); % Close file
- % Perform Fourier Transform (Optional Analysis)
- fftResult = fftshift(fft2(EX));
- energyDistribution = abs(fftResult).^2;
- % Calculate frequency domain kx, ky
- [kx, ky] = meshgrid(linspace(-pi, pi, nx), linspace(-pi, pi, ny));
- % Wave number magnitude and angle
- kr = sqrt(kx.^2 + ky.^2);
- theta = atan2(ky, kx);
- % Angle range bins
- thetaBins = linspace(0, pi/2, 90); % 89 bins
- angleEnergy = zeros(1, length(thetaBins) - 1);
- for i = 1:length(thetaBins) - 1
- mask = (theta >= thetaBins(i)) & (theta < thetaBins(i+1));
- angleEnergy(i) = sum(energyDistribution(mask));
- end
- % Plot energy as a function of angle (polar coordinates)
- figure;
- polarplot(thetaBins(1:end-1), angleEnergy);
- title('Energy Distribution over Angle (0 to 90 degrees)');
- % Normalize energy distribution
- totalEnergy = sum(angleEnergy);
- angleEnergyNormalized = angleEnergy / totalEnergy;
- % Plot normalized energy percentage (polar coordinates)
- figure;
- polarplot(thetaBins(1:end-1), angleEnergyNormalized);
- title('Normalized Energy Percentage over Angle (0 to 90 degrees)');
- % Example analysis for a specified angle
- inputAngleDegrees = 15;
- inputAngleRadians = deg2rad(inputAngleDegrees);
- angleMask = (theta >= 0) & (theta < inputAngleRadians);
- energyInRange = sum(energyDistribution(angleMask));
- fprintf('Energy from 0 to %d degrees: %f\n', inputAngleDegrees, energyInRange);
- end
复制代码
### 主要修改与注释:
- **文件打开失败检查**:在 `fopen` 后立即确认文件成功打开。
- **读取后个数检查**:在每个重要读取步骤后,检查并打印实际读取的元素数量,以便调试。
- **添加了完整的变量声明**:确保所有变量都有适当的声明和使用。
- **四ier变换部分**:保留了原有逻辑,可以根据需要调整或删除。
通过这些修改和调试信息,你应该更容易识别问题并确保数据从文件中正确读取。如果问题仍然存在,请确保文件内容和格式与代码预期一致。
--- 光学专家Gpt |