matlab 读取txt文本,并分析数据

close all;
clc;
clear;

str = ‘E:\matlab\temp_data\‘;
files = dir(strcat(str,‘*.txt‘));
number_files = length(files);

x = zeros(number_files,4526);
y = zeros(number_files,4526);
N = 5;
a = zeros(number_files,N+1);
fun_name = blanks(number_files);

for i=1:number_files
    str1 = sprintf(‘%s%s‘,str,files(i).name);
    [temp,lev] = textread(str1,‘%f,%f‘);
    x(i,:) = temp;
    y(i,:) = lev - lev(1);
    a(i,:) = polyfit(x(i,:),y(i,:),N)
    
    figure;
    plot(x(i,:),y(i,:));
    freq = str2num(files(i).name(regexp(files(i).name,‘\d‘)));
    if (freq < 1e3)
        str2 = sprintf(‘%s%.1f%s‘,‘频率:‘,freq,‘Hz‘);
    elseif(freq <1e6)
        str2 = sprintf(‘%s%.4f%s‘,‘频率:‘,freq/1e3,‘kHz‘);
    elseif(freq < 1e9)
        str2 = sprintf(‘%s%.7f%s‘,‘频率:‘,freq/1e6,‘MHz‘);
    else
        str2 = sprintf(‘%s%.10f%s‘,‘频率:‘,freq/1e9,‘GHz‘);
    end
    title(str2);
    legend(‘温度-幅度‘);
    xlabel(‘温度‘);
    ylabel(‘幅度‘);
end


%%阶数判断
% for i=1:5
%     y2=polyfit(x,y,i);
%     Y=polyval(y2,x);%计算拟合函数在x处的值。
%     if sum((Y-y).^2)<0.1
%         c=i  
%         break;
%     end
% end

读取文本函数:[temp,lev] = textread(str1,‘%f,%f‘);

扫描文件夹函数

str = ‘E:\matlab\temp_data\‘;
files = dir(strcat(str,‘*.txt‘));

相关推荐